Results 1 to 11 of 11

Thread: If statements

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

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: If statements

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

    Does that code actually work?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: If statements

    it does yes

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: If statements

    Does it work for val1 > val2?
    Try it with the following numbers -> 10, 4, 8.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: If statements

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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: If statements

    So this part needs correcting first:
    Java Code:
    if (val1 > val2)
    {
       temp = val1;
       val1 = val2;
       val2 = val3;
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default 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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: If statements

    Depends if it's some sort of algorithm lesson.
    The Math class might be considered cheating...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. If Statements
    By WillHorne in forum New To Java
    Replies: 7
    Last Post: 03-01-2012, 01:47 PM
  2. If Statements
    By katymccl in forum New To Java
    Replies: 6
    Last Post: 02-02-2012, 09:06 AM
  3. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 08:02 PM
  4. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 10:50 PM
  5. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 PM

Posting Permissions

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