This site requires JavaScript, please enable it in your browser!
Greenfoot back
Super_Hippo
Super_Hippo wrote ...

2017/11/8

Problem with ?-: instead of if-else

1
2
danpost danpost

2017/11/8

#
You might try this:
img.drawString(60/tkt<10 ? ""+(60.0/tkt) : ""+(60/tkt));
Super_Hippo Super_Hippo

2017/11/8

#
Nope, that doesn't work. But I am okay with having the if-else and the extra line. It's not a big deal after all, I was just wondering why that happens.
danpost danpost

2017/11/8

#
I just forgot to add the other parameters for 'drawString'. I meant this:
img.drawString(60/tkt<10 ? ""+(60.0/tkt) : ""+(60/tkt), 0, 15);
Super_Hippo Super_Hippo

2017/11/9

#
Oh right, how couldn't I notice that... the result is still 60.0, but it works when adding the (int) in the second part:
img.drawString(60/tkt<10 ? ""+(60.0/tkt) : ""+(int)(60/tkt), 0, 15);
This also works: (the "then-part" with 60 instead of 60.0 and without the '()')
img.drawString(60/tkt<10 ? "" + 60/tkt : "" + (int)(60/tkt), 0, 15);
danpost danpost

2017/11/9

#
Super_Hippo wrote...
Oh right, how couldn't I notice that... the result is still 60.0, but it works when adding the (int) in the second part
Ahh -- you must have 'tkt' as a 'double' type field. Looking back, I do see you referring to its values as '1.0', '2.0', '3.0', etc. I was using it as an 'int' type field above.
You need to login to post a reply.
1
2