+ 11

ternary conditional (java) [Solved]

Hey, Why is it printing "5.0"? Isn't it suppose to print "5"? Since I added "(int)" before it? https://code.sololearn.com/cJL9g3CxZO3G/#java Thank you! [Solved] https://code.sololearn.com/copwkZGEja04/#java

8th Nov 2019, 9:46 PM
KingDaniel2004
KingDaniel2004 - avatar
15 Respostas
+ 3
Alright, so this is pretty redundant... but it works https://code.sololearn.com/cuuwxLGx1Zb8/?ref=app
9th Nov 2019, 1:21 AM
Odyel
Odyel - avatar
+ 10
I'm using a keyboard called "ninja pro" which all the keys are invisible lol Spelling mistake happens 😉 Thanks for your answer 😎👍
8th Nov 2019, 10:20 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 9
voja yes but in here both true and false statement have int... what if I used 5.8? It will print "5" My goal is : If a = 5.0 the output I want is "5" NOT "5.0" But If a = 5.8 the output I want is "5.8" Using thernary conditions... Is it even possible? LOL
8th Nov 2019, 11:06 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 8
Kit Delano Cat oh thank you
8th Nov 2019, 10:23 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 8
Kit Delano Cat So is there is any possible way make it print "5"? In ternary conditional?
8th Nov 2019, 10:52 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 8
No problem LOL I'm trying to find it for 4 hrs... Still finding😔 THANK YOU FOR YOUR HELP
8th Nov 2019, 11:18 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 6
I tired switching (int) to the false side... Still printing "5.0"
8th Nov 2019, 10:14 PM
KingDaniel2004
KingDaniel2004 - avatar
+ 5
Mr. 🍊range It's tried not tired 😁. Btw you are comparing double with int so it will be false and return will be 5.0
8th Nov 2019, 10:17 PM
AÍąJ
AÍąJ - avatar
+ 5
Comparing floats and doubles using == operator is not recommended. So kindly read the Oracle documentation once, may be you will get an idea.
9th Nov 2019, 2:23 AM
Avinesh
Avinesh - avatar
+ 3
Yes, if you make second argument int too: System.out.println((a == (int)a) ? (int)a : (int)a);
8th Nov 2019, 10:55 PM
voja
voja - avatar
+ 3
Mr. 🍊range This works: double a = 5.0; if((int)a - a == 0){ System.out.println((int)a); }else{ System.out.println(a); }
9th Nov 2019, 2:20 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Mr. 🍊range yeah, now I get what you want... I guess I am a bit tired.. 😔
8th Nov 2019, 11:15 PM
voja
voja - avatar
+ 1
java is that weird.
9th Nov 2019, 11:22 AM
Dan Timachov
Dan Timachov - avatar
+ 1
double a = 5.0; System.out.println((a == (int)a) ? String.valueOf((int)a) : a); Shorter and works fine.
9th Nov 2019, 11:57 PM
Hanz Christian Aquino
Hanz Christian Aquino - avatar
0
ÙƒŰŹÙ©
10th Nov 2019, 7:43 PM
Israa
Israa - avatar