Write a program called SmallestLargest.java which outputs the biggest and smalle
ID: 3930361 • Letter: W
Question
Write a program called SmallestLargest.java which outputs the biggest and smallest numbers in a list of numbers entered by the user. Ask the user for a terminating value which should be entered again when they are done inputting the list of numbers. First output the biggest number and then the smallest number. There must be at least 1 number in the list. YOU MUST USE THE IO MODULE FOR INPUT/OUTPUT. Report bad input via IO.reportBadInput() and exit on error. Example: java SmallestLargest 3.00 5.10 6.20 9.00 100.00 17.02 10.73 19.14 3.00 RESULT: 100.00 RESULT: 5.10Explanation / Answer
SmallestLargest.java
import java.util.Scanner;
public class SmallestLargest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter terminating value: ");
double quit = scan.nextDouble();
double n=0;
double min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
while(true){
System.out.print("Enter a number: ");
n = scan.nextDouble();
if(n == quit){
break;
}
if(max < n){
max = n;
}
if(min > n){
min = n;
}
}
System.out.println("Largest value: "+max);
System.out.println("Smallest valueL "+min);
}
}
Output:
Enter terminating value: 3.0
Enter a number: 5.10
Enter a number: 6.20
Enter a number: 9.00
Enter a number: 100.00
Enter a number: 17.02
Enter a number: 10.73
Enter a number: 19.14
Enter a number: 3.00
Largest value: 100.0
Smallest valueL 5.1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.