if and else statements.

D

Devin Panchal

hi,
i am making a program in blue j. it is about a hiring vans. wat i want to
do is;
when the mileage of the van gets to a certain number, i want the terminal
window to say 'the van is on hire'.

this is the follwing code i have created so far, but i cant get the else bit
to work.

public double getservice()
{

if (totalmileage >=10000 && totalmileage <= 10500)
serviceStatus = true;
System.out.println("Van is being serviced");
if (totalmileage >=20000 && totalmileage <= 20500)
System.out.println("Van is being serviced");
if (totalmileage >=30000 && totalmileage <= 30500)
System.out.println("Van is being serviced");
{
else (totalmileage >=0 && totalmileage <=10000)
}
System.out.println("Van does not need servicing")
return totalmileage;

}


how can i make this work? can somone rewrite the code? so it makes sense and
works? do i have to have it as a method? cant it be automatic? the
totalmileage is calculated when the van is hired and returned, as the
imleage is added to the totalmileage.

thanx

devin
 
W

Wendy S

Devin Panchal said:
if (totalmileage >=10000 && totalmileage <= 10500)
serviceStatus = true;
System.out.println("Van is being serviced");

You have a problem here... put {} around the block of statements you want
executed when the condition is true. Otherwise, the way you have it, the
second line stands alone and will be executed whenever this method is
called. Might need to be:
if (totalmileage >=10000 && totalmileage <= 10500) {
serviceStatus = true;
System.out.println("Van is being serviced");
}
else (totalmileage >=0 && totalmileage <=10000)
}

Which "if" does the else go with? Your curly braces aren't done right, you
have this else bit encased inside {}. There are MANY examples of if/else
statements in Java, use your favorite search engine and look at some
examples.
return totalmileage;

Why return totalmileage? You have not changed the value as far as I can
tell, and it must be a class variable (it's not declared in this method, and
it wasn't passed into this method in the method signature).
do i have to have it as a method? cant it be automatic?

What do you mean by automatic? SOMETHING has to call the method to make it
happen. At this point, that's probably a 'main' method in a 'test driver'
type class. Nothing "happens" in the method you posted, except for printing
a message.
 
D

Devin Panchal

thanx for the help,
u helped me a lot. i've sorted it now, but thanx a lot.

dev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top