Reputation: 35
How can I test the percentage in an 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 is able to change the value "tuning" and "gas"
And thats the part i like to add :
if (strecke==33%) {
system.out.println("Test");
}
Could anyone help me?
Upvotes: 2
Views: 7228
Reputation: 17445
You're missing an important piece of input here. 33% of what? Some people here have suggested that it's 33% of one, as in 0.33. That's quite unlikely, since you're doing --gas
at the end, so you're probably working with integer numbers.
I deduce you intend to say '33% of the initial volume'. So that would become:
public void walk() {
boolean walking = true;
int currentGas = initialGas;
while (walking) {
if (currentGas == Math.round((double)initialGas * 0.33)) {
System.out.println("Walker reached at 33% of Gas "+(output*speed)+" meter");
}
if (currentGas <0||energy<0) {
System.out.println("Run out of Gas or Energy!");
walking = false;
break;
}
--currentGas ;
output +=1; }
System.out.println("Distance "+(output*speed)+" meter");
}
That should do it.
Upvotes: 2
Reputation: 1
You don't need a percentage in this particular case, just use plain a plain int. Initialize gas at 100 and then substract one at each iteration of the loop. Replace the code that says "if(gas==33%)" for "if(gas==33)" and that's it.
Upvotes: 0
Reputation: 64632
Create a helper method
private double percentage(double value)
{
return 100 * value;
}
and use it to wrap gas
:
if (percentage(gas) == 33)
{
...
}
Upvotes: 2
Reputation: 2611
Change this:
if (gas==33%)
to:
if (gas==0.33)
(assuming gas is a float/double)
edit: Why am I getting downvoted? 33% is mathematically equivalent to 0.33.
edit: You might also want to do a ranged check like
if (gas > 0.325 && gas < 0.335)
Because just checking for the exact value of 33% is probably not what you wanted.
Upvotes: 2
Reputation: 7901
You're not using % correctly. In Java the % operator is used to find a remainder. For example the following are true:
0 == 6 % 3 (because 6 divided by 3 has no remainder)
1 == 7 % 3 (because 7 divided by 3 has a remainder of 1)
If you want to convert gas to a percentage then you should probably be dividing it by something. For example:
if (gas / fulltank == 0.33)
However, you also need to realize that it may never exactly equal .33. So you probably want to check to see if it's <= .33. If so, then print the message and set a boolean variable to remember that you already printed it.
Also note that if gas is an integer type then you need to first convert it to a float:
if ((float)gas / fulltank == 0.33)
Upvotes: 1
Reputation: 122421
There is no percentage operator in Java (it is used as the Modulo operator - the amount left over after a division) so you have to calculate the percentage yourself.
Unfortunately, from your code, I cannot work out what gas
should be compared to in order to determine if it's 33% the way there.
Upvotes: 3