Prompt for and read a number between 1 and 5. Repeat this step until the input i
ID: 3805954 • Letter: P
Question
Prompt for and read a number between 1 and 5. Repeat this step until the input is 1..5. Repeat the following multiple times according to the number read in step 1. a. Read in a list of integers ending with a 0. The 0 marks the end of the input and is not considered part of the list b. Print the largest and smallest integers in the list. c. If only a zero appears in the list, print an error message Here is a pseudo-code solution. Begin repeat prompt user to enter a number from 1 to 5 read number until number is from 1 to 5 repeat number times decrement number read input if input = 0 print error message else set min to input set max to input read input repeat while input is not = 0 if input max then set max to input end if end if read input end repeat print max print min end if end repeat end For this assignment, you are to convert the algorithm into a non-iterative solution (no loops) and code it in Java following the coding style requirements posted for the class. You may design your solution to include as many methods as you wish. Submit only your Java file(s). No class files will be accepted. To receive credit, your program must compile. No exceptions. You can assume that all input values are valid integers.Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class MinMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number from 1-5: ");
int number = sc.nextInt();
// input validation
while(number<1 || number > 5){
System.out.print("Enter a number from 1-5: ");
number = sc.nextInt();
}
while(number > 0){
number = number - 1;
System.out.println("Enter list of integers (0 to stop): ");
System.out.print("Enter input: ");
int input = sc.nextInt();
if(input == 0){
System.out.println("ERROR: At least one number should be greater than 0");
}else{
int min = input;
int max = input;
while(true){
System.out.print("Enter input: ");
input = sc.nextInt();
if(input == 0)
break;
if(input > max)
max = input;
if(input < min)
min = input;
}
System.out.println("Max: "+max);
System.out.println("Min: "+min);
System.out.println();
}
}
}
}
/*
Sample run:
Enter a number from 1-5: 9
Enter a number from 1-5: 2
Enter list of integers (0 to stop):
Enter input: 3
Enter input: 1
Enter input: 2
Enter input: 0
Max: 3
Min: 1
Enter list of integers (0 to stop):
Enter input: -1
Enter input: 2
Enter input: 0
Max: 2
Min: -1
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.