Question
Can someone please help me with this? I am trying to write a java code that can sort 4 numbers in an increasing orders. I
Can someone please help me with this? I am trying to write a java code that can sort 4 numbers in an increasing orders. I have written the following code but I dont know whats wrong with it. Sometimes it works and it actuallt sort the numbers other times it doesnt. For example if I enter 9 10 4 -2 it gives me 9 4 -2 10.
public class firstHomework {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter four integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int number3 = input.nextInt();
int number4 = input.nextInt();
if (number1 > number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}
if (number2 > number3) {
int temp = number2;
number2 = number3;
number3 = temp;
}
if (number3 > number4) {
int temp = number3;
number3 = number4;
number4 = temp;
}
System.out.println("The sorted numbers are "
+ number1 + " " + number2 + " " + number3 + " " + number4 + " ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started