I need help with this java code assignment. I have a good idea of where to start
ID: 3883698 • Letter: I
Question
I need help with this java code assignment. I have a good idea of where to start but feel like I'm missing steps. Can this be solved with the java.util.Scanner? please post an example and explanation if you can.
Write a program that reads three integer inputs into variables. Display the input values in both the ordered entered and in sorted order. This program should sort the numbers so that value1 <= value2 <= value3. Do not use any sort functions to do this, use if conditionals.
Thank you
Explanation / Answer
import java.util.Scanner;
public class SortNumbers {
public static void main(String args[]) {
int num1, num2, num3, temp;
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.print("Enter first no: ");
num1 = sc.nextInt();
System.out.println();
System.out.print("Enter second no: ");
num2 = sc.nextInt();
System.out.println();
System.out.print("Enter third no: ");
num3 = sc.nextInt();
if(num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
if(num2 > num3 ) {
temp = num2;
num2 = num3;
num3 = temp;
}
if(num1 > num2 ) {
temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println("Elements in sorted order are: "+ num1+" " + num2 +" "+ num3);
}
}