1. Member
Join Date
Nov 2012
Posts
13
Rep Power
0

## If statements

I am working on a program that prompts the user to input 3 numbers and it evaluates the input and displays the numbers in nondecending order. My question is I have a couple lines of code that a classmate helped me write but he didn't offer me an explanation as to why this works, can anyone break it down for me?

if (val3 <= val1)
System.out.println(val3 + " " + val1 + " " + val2);

else if (val1 <= val3 && val3 <= val2)
System.out.println(val1 + " " + val3 + " " + val2);

else
System.out.println(val1 + " " + val2 + " " + val3);

here is my complete code:

import java.util.*;

public class Evaluate_3
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{

double val1, val2, val3;
double temp;

System.out.print("Enter three numbers with a space seperating each one: ");

val1 = console.nextDouble();
val2 = console.nextDouble();
val3 = console.nextDouble();

if (val1 > val2)
{
temp = val1;
val1 = val2;
val2 = val3;
}
System.out.printf("Your numbers in non decending order: ");

if (val3 <= val1)
System.out.println(val3 + " " + val1 + " " + val2);

else if (val1 <= val3 && val3 <= val2)
System.out.println(val1 + " " + val3 + " " + val2);

else
System.out.println(val1 + " " + val2 + " " + val3);
}
}

2. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: If statements

Please use [code] tags [/code] when posting code, as that's almost unreadable without them.

Does that code actually work?

3. Member
Join Date
Nov 2012
Posts
13
Rep Power
0

it does yes

4. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: If statements

Does it work for val1 > val2?
Try it with the following numbers -> 10, 4, 8.

5. Member
Join Date
Nov 2012
Posts
13
Rep Power
0

## Re: If statements

Ah good catch no it does not work, in that case it lists 4, 8, 8

6. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: If statements

So this part needs correcting first:
Java Code:
```if (val1 > val2)
{
temp = val1;
val1 = val2;
val2 = val3;
}```

7. Member
Join Date
Nov 2012
Posts
13
Rep Power
0

## Re: If statements

Ok, that's kind of the part I was confused on, my counterpart didn't want to explain to me why he just wanted to write it and get it over with. I will read the section on if statements again in my textbook and post the result once it is corrected. Thank you for looking.

8. ## Re: If statements

Have a look at the Math.min( ... ) and Math.max( ... ) methods; also note that a+b+c-M-m (where M and m and the maximum and minimum of the three numbers a, b and c) results in the third 'middle' number.

kind regards,

Jos

9. Member
Join Date
Nov 2012
Posts
13
Rep Power
0

## Re: If statements

I changed up everything, rewrote my if and else statements and I THINK I have got it so it is right (seems to work with any combination)

Java Code:
```import java.util.Scanner;

public class Evaluate_3 {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");

double A = input.nextDouble();
System.out.print("Enter a second number: ");

double B = input.nextDouble();
System.out.print("Enter a third number: ");

double C = input.nextDouble();
System.out.println("The numbers in non decending order are: ");

displaySortedNumbers(A, B, C);

}
public static void displaySortedNumbers(double A, double B, double C)
{

if ( (A <= B) && (A <= C) )
{
System.out.print(A);

if (B <= C)
System.out.println(" " + B + " " + C);

else
System.out.println(" " + C + " " + B);
}

else if ( (B <= A) && (B <= C) ) {
System.out.print(B);

if (A < C)
System.out.println(" " + A + " " + C);

else
System.out.println(" " + C + " " + A);
}

else if ( (C <= A) && (C <= B) ) {
System.out.print(C);

if (A < B)
System.out.println(" " + A + " " + B);

else
System.out.println(" " + B + " " + A);
}
}
}```
Last edited by Tolls; 11-15-2012 at 10:42 AM. Reason: Correct tags

10. ## Re: If statements

grmph ...

Java Code:
```int max= Math.max(A, Math.max(B, C));
int min= Math.min(A, Math.min(B, C));
int mid= A+B+C-max-min;```
kind regards,

Jos

11. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: If statements

Depends if it's some sort of algorithm lesson.
The Math class might be considered cheating...:)

#### Posting Permissions

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