java - How to test percentage in an if statement? -
how can test percentage in if statement?
for example: package walker; public class walker { int tuning = 10; int speed = 0; int gas = 100; int energy = 100; int time = 1; int strecke = 0; boolean test = true; boolean beschleunigung = false; walker() { tuning = 10; gas = 0; energy = 100; } public void setgas(int x) { gas = x; } public void setspeed(int x) { tuning = x; } public void walk() { boolean walking = true; while (walking) { if (speed<10) { beschleunigung = true; }else { beschleunigung = false; } if (beschleunigung==true) { speed +=1+(tuning); energy -=1; } strecke = (speed*time); speed = (strecke/time); gas -= 1; energy -= 1; time +=1; if (gas<1||energy<1){ break; } } system.out.println("distance "+(strecke)+" meter"); system.out.println("gas left :"+gas); system.out.println("energy left : "+energy); } }
the _user able change value "tuning" , "gas"
and thats part add :
if (strecke==33%) { system.out.println("test"); }
could me?
you're not using % correctly. in java % operator used find remainder. example following true:
0 == 6 % 3 (because 6 divided 3 has no remainder)
1 == 7 % 3 (because 7 divided 3 has remainder of 1)
if want convert gas percentage should dividing something. example:
if (gas / fulltank == 0.33)
however, need realize may never equal .33. want check see if it's <= .33. if so, print message , set boolean variable remember printed it.
also note if gas integer type need first convert float:
if ((float)gas / fulltank == 0.33)
Comments
Post a Comment