The purpose of this assignment was the find the smallest and largest of the integer(User inputs 5 entries)
I am a beginner Java programmer with no prior experience. Is there an easier way to do this without a ton of if/else statements?
Forgive for the dirty code.
//Scanner Object
Scanner input = new Scanner(System.in);
int num1, num2, num3, num4, num5;
int large;
int small;
System.out.print("Enter Num1: ");
num1 = input.nextInt();
System.out.print("Enter Num2: ");
num2 = input.nextInt();
System.out.print("Enter Num3: ");
num3 = input.nextInt();
System.out.print("Enter Num4: ");
num4 = input.nextInt();
System.out.print("Enter Num5: ");
num5 = input.nextInt();
//If/Else statements (SMALL)
if (num1<num2 && num1<num3 && num1<num4 && num1<num5) {
small = num1;
}
else if (num2<num1 && num2<num3 && num2<num4 && num2<num5) {
small = num2;
}
else if (num3<num1 && num3<num2 && num3<num4 && num3<num5) {
small = num3;
}
else if (num4<num1 && num4<num2 && num4<num3 && num4<num5) {
small = num4;
}
else {
small = num5;
}
//Large
if (num1>num2 && num1>num3 && num1>num4 && num1>num5) {
large = num1;
}
else if (num2>num1 && num2>num3 && num2>num4 && num2>num5) {
large = num2;
}
else if (num3>num1 && num3>num2 && num3>num4 && num3>num5) {
large = num3;
}
else if (num4>num1 && num4>num2 && num4>num3 && num4>num5) {
large = num4;
}
else {
large = num5;
}
//Display: Use printf when using a format limiter (%)
System.out.printf("The smallest integer: %d\n", small);
System.out.printf("The largest integer: %d\n", large);
}// end of main
}