Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to create a program that takes 3 numbesr from the user and then displays

ID: 3542549 • Letter: I

Question

I have to create a program that takes 3 numbesr from the user and then displays them in increasing order. I wrote a program, however any digit over 100 does not display correctly

Here is my code

import java.util.Scanner;

public class program1 {
    public static void main(String[] args)
    {
        System.out.println("Hello There");
        System.out.println("Please enter three non negative integers");
        System.out.println("Enter the first non negative number");
        
        //Define all variables
        
        int a, b, c;
        Scanner Keyboard = new Scanner(System.in);
        a = Keyboard.nextInt();
        
        System.out.println("Enter the second non negative number");
        b = Keyboard.nextInt();
        
        System.out.println("Enter the third non negative number");
        c = Keyboard.nextInt();
        
        //soluton can only be (a,b,c), (a,c,b), (b,a,c), (b,c,a), (c,a,b), (c,b,a)
        //remember to use curly brackets for multiple else-ifs
        if (a < b && b < c)
        {
        System.out.println("Here are your numbers sorted");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    
        }else if (a < c && c < b){
        System.out.println("Here are your numbers sorted");
        System.out.println(a);
        System.out.println(c);
        System.out.println(b);
        
        }else if (b < a && a < c)
            {System.out.println("Here are your numbers sorted");
            System.out.println(b);
            System.out.println(a);
            System.out.println(c);
        
            } else if (b < c && c < a)
        {System.out.println("Here are your numbers sorted");
        System.out.println(b);
        System.out.println(c);
        System.out.println(a);
        
        }else if (c < a && a < b)
            {System.out.println("Here are your numbers sorted");
            System.out.println(c);
            System.out.println(a);
            System.out.println(b);
        
            }else if (c < b && b < a)
                System.out.println("Here are your numbers sorted");
                System.out.println(c);
                System.out.println(b);
                System.out.println(a);
                
    }

}

-------------------------------------------

OUTPUT:

Hello There
Please enter three non negative integers
Enter the first non negative number
4
Enter the second non negative number
200
Enter the third non negative number
23
Here are your numbers sorted
4
23
200
23
200
4


-----------------------------


1) Can anyone explain to me why this method wont work? Why is it listing C,B,A  and B,C,A at the same time???

2) And also does this solution here have the same problem as the one I initially created above?

www.chegg.com/homework-help/java-introduction-to-problem-solving-and-programming-6th-edition-chapter-3-problem-2pp-solution-9780132162708


Thanks


Explanation / Answer

Here is the fix to this.


Reason is it is trying to evaluate more if else after finding the corresponding if and elses. So exit the program once an if block is executed.




Code


import java.util.Scanner;


public class program1 {

public static void main(String[] args)

{

System.out.println("Hello There");

System.out.println("Please enter three non negative integers");

System.out.println("Enter the first non negative number");


//Define all variables


int a, b, c;

Scanner Keyboard = new Scanner(System.in);

a = Keyboard.nextInt();


System.out.println("Enter the second non negative number");

b = Keyboard.nextInt();


System.out.println("Enter the third non negative number");

c = Keyboard.nextInt();


//soluton can only be (a,b,c), (a,c,b), (b,a,c), (b,c,a), (c,a,b), (c,b,a)

//remember to use curly brackets for multiple else-ifs

//suppose we gave 4 2 3 as input.

//so, a = 4, b = 2 and c = 3


if (a < b && b < c)

{

System.out.println("Here are your numbers sorted");

System.out.println(a);

System.out.println(b);

System.out.println(c);

System.exit(1);


}else if (a < c && c < b){

System.out.println("Here are your numbers sorted");

System.out.println(a);

System.out.println(c);

System.out.println(b);

System.exit(1);


}else if (b < a && a < c)

{System.out.println("Here are your numbers sorted");

System.out.println(b);

System.out.println(a);

System.out.println(c);

System.exit(1);


} else if (b < c && c < a)

{System.out.println("Here are your numbers sorted");

System.out.println(b);

System.out.println(c);

System.out.println(a);

System.exit(1);


}else if (c < a && a < b)

{System.out.println("Here are your numbers sorted");

System.out.println(c);

System.out.println(a);

System.out.println(b);

System.exit(1);


}else if (c < b && b < a)

System.out.println("Here are your numbers sorted");

System.out.println(c);

System.out.println(b);

System.out.println(a);

System.exit(1);


}


}