public static void main(String[] args) { Scanner scanner = new Scanner(System.in
ID: 3670458 • Letter: P
Question
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n, count = 0, min = 0, max = 0, total = 0; System.out.println("Enter up to ten integers."); System.out.println("To stop before ten, enter a zero."); do { System.out.print("Next input;"); n = scanner.nextInt(); if(n !=0) { if(count == 0) {//This is the first input. Initialize min and max. min = n; max = n;} else {//Additional input. Update min and max if (n max) { max = n;}} total += n; count ++;}} while (n ! = 0); if(count > 0) {//output extremes and average System.out.println(" The minimum is " + min); System.out.println(" the maximum is " + max); DecimalFormat decimalFormatter = new DecimalFormat("0.####"); double average = (double)total/count; System.out.println("The average is " + decimalFormatter. format(average));} else} { System.out.println("No inputs were entered");}Explanation / Answer
public static void main(String[] args) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
System.out.println("Enter upto 10 integers.");
System.out.println("TO stop before enter 0");
int i=0,total=0;
while (i<10) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
i++;
total+=val;
}
System.out.println("min: " + min);
System.out.println("max: " + max);
System.out.println("Average : "+(double)total/i);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.