Results 1 to 5 of 5
  1. #1
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default main method returned to an earlier if statment.

    Dear all,

    This program should display the sum, average, product, smallest and largest of the numbers.

    Java Code:
    //Practice6
    //Displays the sum, average, product, smallest and largest of the numbers
    
    import java.util.Scanner;
    
    public class Practice6
    {
    	public static void main( String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Enter an integer: ");
    		int integer1 = sc.nextInt();
    		System.out.print("Enter an integer: ");
    		int integer2 = sc.nextInt();
    		System.out.print("Enter an integer: ");
    		int integer3 = sc.nextInt();
    		System.out.printf("The sum is: %d\nThe average is: %d\nThe product is: %d\n", integer1 + integer2 + integer3 , (integer1 + integer2 + integer3) / 3 , integer1 * integer2 * integer3 );
    		if ( integer1 > integer2 )
    			if ( integer2 > integer3 )
    			System.out.println("The first integer is the largest and the third integer is the smallest");
    			
    		if ( integer1 > integer3 )
    			if ( integer3 > integer2 )
    			System.out.println("The first integer is the largest and the second integer is the smallest");
    			
    		if ( integer2 > integer1 )
    			if ( integer1 > integer3 )
    			System.out.println("The second integer is the largest and the third integer is the smallest");
    		
    		if ( integer2 > integer3 )
    			if ( integer3 > integer1 )
    			System.out.println("The second integer is the largest and the first integer is the smallest");
    			
    		if ( integer3 > integer1 )
    			if ( integer1 > integer2 )
    			System.out.println("The third integer is the largest and the second integer is the smallest");
    			
    		if ( integer3 > integer2 )
    			if ( integer2 > integer1 )
    			System.out.println("The third integer is the largest and the first integer is the smallest");
    	}
    }
    The problem is when I enter
    The first integer: 7
    The second integer: 5
    The third integer: 9
    The program displays "The third integer is the largest and the second integer is the smallest" then displays "The first integer is the largest and the second integer is the smallest".
    I do not understand how? the main method returned to the IF statement in the line 23. How or why?

    Thank you all

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: main method returned to an earlier if statment.

    You should use curly brackets after every if statement.

    Have you tried stepping through this with a debugger to see what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: main method returned to an earlier if statment.

    Your code does not show the described behaviour.

    And the code that you gave works exactly as it should:
    Enter an integer: 7
    Enter an integer: 5
    Enter an integer: 9
    The sum is: 21
    The average is: 7
    The product is: 315
    The third integer is the largest and the second integer is the smallest

    So please make sure to post the correct code and error description.

    Also a few things that you might want to keep in mind:
    a) curly brackets help to make code easier to read.
    b) If you just want to have one decision, then you should use if else if constructs.
    c) It might be easier to read to use something like if (integer1 > integer2 && integer2 > integer3) instead of 2 separate if statements. Or at least change the logic so you have clean if else statements where you do not check something 2 times.

    Konrad

  4. #4
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: main method returned to an earlier if statment.

    Thank you KevinWorkman.

    Thank you konard. You are right Konard. the code is work correctly. the problem was not in the code that insert here. I faced this problem with my code and I tried to solve it and I solved it BUT I forgot to compile the code after solving the problem!!! and I was running the past code not to compile then running the corrected one!!!

    Thank you all

  5. #5
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: main method returned to an earlier if statment.

    Konrad, I am learning java now and the book did not explain if else statements till now.

Similar Threads

  1. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  2. Replies: 1
    Last Post: 10-21-2011, 10:51 AM
  3. Earlier date
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 3
    Last Post: 02-07-2010, 06:04 PM
  4. Cant we have two values returned from a method in a class
    By jaiminparikh in forum Advanced Java
    Replies: 7
    Last Post: 03-21-2009, 03:20 AM

Posting Permissions

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