Reputation: 11
I have tried everything, but i cannot get this code to print the 5 numbers in order from greatest to least. Any help will be appriciated. The java website tells me to write more so here it goes. I would use a loop to achieve this, but the task requires that loops are not used. I am having trouble with printing out these 5 numbers from greatest to least without using loops. Please help
import java.util.Scanner;
public class InOrder{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int hi=0;
int hi2=0;
int mid=0;
int low=0;
int low2=0;
System.out.println("Enter 5 variables:");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int num4 = input.nextInt();
int num5 = input.nextInt();
//Finds the highest number
if ((num1 >= num2) && (num1 >= num3) && (num1 >= num4) && (num1 >= num5)) {
hi = num1;
} else if ((num2 >= num1) && (num2 >= num3) && (num2 >= num4) && (num2 >= num5)) {
hi = num2;
} else if ((num3 >= num1) && (num3 >= num2) && (num3 >= num4) && (num3 >= num5)) {
hi = num3;
} else if ((num4 >= num1) && (num4 >= num2) && (num4 >= num3) && (num4 >= num5)) {
hi = num4;
} else if ((num5 >= num1) && (num5 >= num2) && (num5 >= num3) && (num5 >= num4)) {
hi = num5;
}
// Finds Second Highest
if ((num1 > mid) && (num1 > low) && (num1 > low2) && (num1 < hi)) {
hi2 = num1;
} else if ((num2 > mid) && (num2 > low) && (num2 > low2) && (num2 < hi)) {
hi2 = num2;
} else if ((num3 > mid) && (num3 > low) && (num3 > low2) && (num3 < hi)) {
hi2 = num3;
} else if ((num4 > mid) && (num4 > low) && (num4 > low2) && (num4 < hi)) {
hi2 = num4;
} else if ((num5 > mid) && (num5 > low) && (num5 > low2) && (num5 < hi)) {
hi2 = num5;
}
//Finds the middle number
if ((num1 < hi) && (num1 < hi2) && (num1 > low2) && (num1 > low)) {
mid = num1;
} else if ((num2 < hi) && (num2 < hi2) && (num2 > low2) && (num2 > low)) {
mid = num2;
} else if ((num3 < hi) && (num3 < hi2) && (num3 > low2) && (num3 > low)) {
mid = num3;
} else if ((num4 < hi) && (num4 < hi2) && (num4 > low2) && (num4 > low)) {
mid = num4;
} else if ((num5 < hi) && (num5 < hi2) && (num5 > low2) && (num5 > low)) {
mid = num5;
}
//Finds the lowest number
if ((num1 <= num2) && (num1 <= num3) && (num1 <= num4) && (num1 <= num5)) {
low = num1;
} else if ((num2 <= num1) && (num2 <= num3) && (num2 <= num4) && (num2 <= num5)) {
low = num2;
} else if ((num3 <= num1) && (num3 <= num2) && (num3 <= num4) && (num3 <= num5)) {
low = num3;
} else if ((num4 <= num1) && (num4 <= num2) && (num4 <= num3) && (num4 <= num5)) {
low = num4;
} else if ((num5 <= num1) && (num5 <= num2) && (num5 <= num3) && (num5 <= num4)) {
low = num5;
}
//Finds Second Lowest
if ((num1 < mid) && (num1 > low) && (num1 < hi2) && (num1 < hi)) {
low2 = num1;
} else if ((num2 < mid) && (num2 > low) && (num2 < hi2) && (num2 < hi)) {
low2 = num2;
} else if ((num3 < mid) && (num3 > low) && (num3 < hi2) && (num3 < hi)) {
low2 = num3;
} else if ((num4 < mid) && (num4 > low) && (num4 < hi2) && (num4 < hi)) {
low2 = num4;
} else if ((num5 < mid) && (num5 > low) && (num5 < hi2) && (num5 < hi)) {
low2 = num5;
}
System.out.println("The variables from greatest to least are:");
System.out.print("" + hi);
System.out.print("," + hi2);
System.out.print("," + mid);
System.out.print("," + low2);
System.out.print("," + low);
}
}
Upvotes: 1
Views: 82
Reputation: 2168
Also with Roman Graf's answer You need to include this at the top of the document
import java.util.Arrays;
Try doing something like this:
public class NoLoopSort {
static int[] values = new int[] { 4, 2, 5, 1, 3 };
public static void main(String[] args) {
printLower(0, values.length - 1);
printLower(0, values.length - 1);
printLower(0, values.length - 1);
printLower(0, values.length - 1);
printLower(0, values.length - 1);
}
private static void printLower(int start, int end) {
if (start == end) {
// this is currently the lowest number in array, print it.
System.out.println(values[start]);
// make current index biggest possible
values[start] = Integer.MAX_VALUE;
} else {
if (values[start] < values[end]) {
printLower(start, end - 1);
} else if (values[start] >= values[end]) {
printLower(start + 1, end);
}
}
}
}
The above is a recursive swapping function that basically just starts off at the beginnning and then checks if the next number in an array is larger or smaller, if its smaller than it swaps the two and starts over, otherwise it moves on to the next number.
[edit] java code replaced with tested version.
Upvotes: 2
Reputation: 239
You don't want to use loops?
System.out.println(Arrays.sort(new int[]{num1,num2,num3,num4,num5}));
A little bit cheaty. You create an Array and sort this array using APIs. Or is it forbidden to call functions that use loops?
Upvotes: 0