# Thread: main method returned to an earlier if statment.

1. Member
Join Date
May 2014
Posts
32
Rep Power
0

## 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. ## 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?

3. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
4

## 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.

4. Member
Join Date
May 2014
Posts
32
Rep Power
0

## 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. Member
Join Date
May 2014
Posts
32
Rep Power
0

## 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.

#### Posting Permissions

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