Create a java program that goes into a loop and then (continuously) prompts the
ID: 3585912 • Letter: C
Question
Create a java program that goes into a loop and then (continuously) prompts the user for three values. The user is then asked if he/she would like to select the largest of the three values or the smaller of the three values. The program should then display the correct result, and prompt the user to go again or exit. The app should also handle the situation in which all three values are equal by stating "val1,val2,val3 are equal". Here is some pseudocode that shows a comparison of two values, val1 and val2:
val1 = 3
val2 = 5
if (val1 is > val2)
print val1 is greater than val2
else
print val2 is greater than val1
Explanation / Answer
ThreeNumbersMinMax.java
import java.util.Scanner;
public class ThreeNumbersMinMax {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char ch = 'y';
while(ch !='n' && ch !='N') {
System.out.println("Enter the number 1: ");
int n1 = scan.nextInt();
System.out.println("Enter the number 2: ");
int n2 = scan.nextInt();
System.out.println("Enter the number 3: ");
int n3 = scan.nextInt();
System.out
.println("Enter you choice: 1.Find Largest 2. Find Smallest ");
int choice = scan.nextInt();
if (choice == 1) {
if (n1 == n2 && n2 == n3) {
System.out.println("Three values are equals");
} else {
if (n1 > n2 && n1 > n3) {
System.out.println("Largest Value: " + n1);
} else {
if (n2 > n3) {
System.out.println("Largest Value: " + n2);
} else {
System.out.println("Largest Value: " + n3);
}
}
}
} else {
if (n1 == n2 && n2 == n3) {
System.out.println("Three values are equals");
} else {
if (n1 < n2 && n1 < n3) {
System.out.println("Smallest Value: " + n1);
} else {
if (n2 < n3) {
System.out.println("Smallest Value: " + n2);
} else {
System.out.println("Smallest Value: " + n3);
}
}
}
}
System.out.println("Do you want to continure (y or n): ");
ch = scan.next().charAt(0);
}
}
}
Output:
Enter the number 1:
3
Enter the number 2:
2
Enter the number 3:
4
Enter you choice: 1.Find Largest 2. Find Smallest
1
Largest Value: 4
Do you want to continure (y or n):
y
Enter the number 1:
3
Enter the number 2:
2
Enter the number 3:
4
Enter you choice: 1.Find Largest 2. Find Smallest
2
Smallest Value: 2
Do you want to continure (y or n):
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.