PDA

View Full Version : Help with a Java Program


luboldja
11-12-2009, 03:21 AM
Yea, im making a small java driving game for my school, and it is INSANELY simple, but this line of code is giving me hell a dificulty. Can anyone tell me why it like... doesn't work, i'll paste my whole class in here if u need more, but its just this method im having trouble with.

public void drive(double distance){

if(gas > 0){
if(isCarStarted = true){
gas = gas - (distance/efficiency);
System.out.println("You have driven " + distance + " miles. You have " + gas + " gallons of gas left.");
}else{
System.out.println("You cannot drive.");

}
}
}

I dont know why it won't print "You cannot drive." to the screen when gas is not greater than 0 OR isCarStarted = false. Can anyone help?

shadowmaniac
11-12-2009, 04:34 AM
You posted in the wrong forum and use the code tags.
As for your problem, it's because of

if(isCarStarted = true)

You're assigning "true" to isCarStarted as opposed to checking if it's true.

tomws
11-12-2009, 04:46 AM
You posted in the wrong forum

It's worse than that. He cross-posted x4. Apparently literacy and common sense aren't required for the class where this homework was assigned.

oracleguy
11-12-2009, 05:05 AM
Please do not cross post. Read CF's posting rules (http://www.codingforums.com/rules.htm) and guidelines. (http://www.codingforums.com/postguide.htm)