Results 1 to 11 of 11
  1. #1
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default "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)));
    }
    }
    }
    }
    }
    }

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    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)));
            }
    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.
    And another thing is that you have about 3 extra } at the end of your code
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    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
    double moped, days, time;
    double moped = scanner.next( );
    double days = scanner.next( );
    double time = scanner.next( );
    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()

    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.

  5. #5
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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)));
    					}
    				}
    			
    		
    	}
    }

  6. #6
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    that is a good change, but you have } missing now, and one thing is that in :
    else if (time <= 3) {
    System.out.println("Cost is $30");
    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.

    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.

  7. #7
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    If (day=1) and (time<= 3), the the price is set. How would you suggest I change it?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    if (moped = 1) {
    That is an assignment and not a logical comparison.

  9. #9
    to_devendra is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    if (moped = 1) {
    should be
    if (moped == 1) {

    And one } is missing

  10. #10
    to_devendra is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    if (moped = 1) {
    should be
    if (moped == 1) {

    And one {is missing

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 04:44 AM
  2. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 PM
  3. Replies: 3
    Last Post: 05-25-2009, 09:50 PM
  4. Replies: 0
    Last Post: 03-16-2009, 12:53 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •