Results 1 to 11 of 11
Thread: "If statement" issue
- 04-12-2011, 04:03 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
"If statement" issue
I am writing a program that determines the price to rent two different mopeds. The program will not compile and I'm not sure where to go from here.
The 50cc mopette is 15$ for the first 3 hours, and $2.50 an hour after the first 3 hours on a weekday. It is $30 for the first 3 hourse and $7.50 an hour after that on the weekend.
The 250cc mohawk is 25$ for the first 3 hours, and $3.50 an hour after the first 3 hours on a weekday. It is $35 for the first 3 hourse and $8.50 an hour after that on the weekend.
import java.text.*;
import java.util.*;
import java.lang.*;
class Ch5Pr21 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double moped, days, time;
System.out.println("Choose your moped: Enter 1 for 50cc Mopette or 2 for 250 cc Mohawk.");
double moped = scanner.next( );
System.out.println("Weekday or weekend: Enter 1 for weekday or 2 for weekend.");
double days = scanner.next( );
System.out.println("How many hours?");
double time = scanner.next( );
if (moped = 1) {
if (days = 1) {
if (time <= 3)
System.out.println("Cost is $15");
} else {
System.out.println("Cost is $" + (15+((time-3)*2.5)));
}
} else {
if (time <= 3) {
System.out.println("Cost is $30");
} else {
System.out.println("Cost is $" + (30+((time-3)*7.5)));
}
} else {
if (days = 1) {
if (time <= 3)
System.out.println("Cost is $25");
} else {
System.out.println("Cost is $" + (25+((time-3)*3.5)));
}
} else {
if (time <= 3)
System.out.println("Cost is $35");
} else {
System.out.println("Cost is $" + (35+((time-3)*8.5)));
}
}
}
}
}
}
- 04-12-2011, 04:08 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Please use CODE or PHP tags when posting code, so it is readable.
If you get a compiler error, please post the full compiler error message.
Java error messages usually tell you pretty much what is wrong and where.
- 04-12-2011, 04:15 PM #3
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
first thing I noticed is that you have more than one else after the first main if, and that may never work, since an if takes one else only, if you want more than one else then you have to set a condition on every one until the last one when none of the conditions is satisfied.Java Code:if (moped = 1) { if (days = 1) { if (time <= 3) System.out.println("Cost is $15"); } else { System.out.println("Cost is $" + (15+((time-3)*2.5))); } } else { if (time <= 3) { System.out.println("Cost is $30"); } else { System.out.println("Cost is $" + (30+((time-3)*7.5))); } } else { if (days = 1) { if (time <= 3) System.out.println("Cost is $25"); } else { System.out.println("Cost is $" + (25+((time-3)*3.5))); } } else { if (time <= 3) System.out.println("Cost is $35"); } else { System.out.println("Cost is $" + (35+((time-3)*8.5))); }
And another thing is that you have about 3 extra } at the end of your codeClick on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 04:29 PM #4
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
To help you more efficiently I copied your code and tried it.
1) It seems that you have some problems if setting the braces in their right places, this what is causing the biggest trouble with the code.
2) your initialized the three double in the main and then you did that again in the body, and that makes an error as well
3) your set the doubles to read scanner.next() and that won't work since you have to specify that you will read a double by using scanner.nextDouble()double moped, days, time;
double moped = scanner.next( );
double days = scanner.next( );
double time = scanner.next( );
I fixed these three problem in my code and it just worked perfectly. Try with what I suggest and if you faced any problem understanding or fixing them I am glad to help :)Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 04:44 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
I tried to make the changes. This is what I have now, but still a no-go.
Java Code:import java.text.*; import java.util.*; import java.lang.*; class Ch5Pr21 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double moped, days, time; System.out.println("Choose your moped: Enter 1 for 50cc Mopette or 2 for 250 cc Mohawk."); moped = scanner.nextdouble( ); System.out.println("Weekday or weekend: Enter 1 for weekday or 2 for weekend."); days = scanner.nextdouble( ); System.out.println("How many hours?"); time = scanner.nextdouble( ); if (moped = 1) { if (days = 1) { if (time <= 3) { System.out.println("Cost is $15"); } else { System.out.println("Cost is $" + (15+((time-3)*2.5))); } else if (time <= 3) { System.out.println("Cost is $30"); } else { System.out.println("Cost is $" + (30+((time-3)*7.5))); } else if (days = 1){ if (time <= 3) System.out.println("Cost is $25"); } else { System.out.println("Cost is $" + (25+((time-3)*3.5))); } else if (time <= 3){ System.out.println("Cost is $35"); } else { System.out.println("Cost is $" + (35+((time-3)*8.5))); } } } }
- 04-12-2011, 04:49 PM #6
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
that is a good change, but you have } missing now, and one thing is that in :
You mean if (day = 1) is false then if (time <= 3) ??? I don't think is is what you mean, so change that to perform the right thing you want it to do.else if (time <= 3) {
System.out.println("Cost is $30");
Another HINT is that when you want to compare double int or float you should use double equal signs == and not = (= is for boolean us)Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 06:47 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
If (day=1) and (time<= 3), the the price is set. How would you suggest I change it?
- 04-12-2011, 11:16 PM #8
That is an assignment and not a logical comparison.Java Code:if (moped = 1) {
- 04-12-2011, 11:58 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
if (moped = 1) {
should be
if (moped == 1) {
And one } is missing
- 04-13-2011, 12:00 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
if (moped = 1) {
should be
if (moped == 1) {
And one {is missing
- 04-13-2011, 12:39 AM #11
Similar Threads
-
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 03:44 AM -
Simple "if" statement problem....compiling error.
By CYANiDE in forum New To JavaReplies: 4Last Post: 10-14-2009, 09:56 PM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
"Cached Item Was Locked" causing Select Statement: Hibernate + EHCache
By cloutierm in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 11:53 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks