62.8k views
4 votes
4.12 LAB: Varied amount of input data Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics. Ex: When the input is: 15 20 0 5 -1 the output is: 10 20 You can assume that at least one non-negative integer is input. Keeps saying my code is incorrect

mport java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int total = 0;
int count = 0;
int maximum = 0;
int number;
while (true) {
number = scnr.nextInt();
}
if (number < 0) {
System.out.println(break);
}
if (count == 0) {
System.out.println("maximum = number");
}
if (number > maximum) {
System.out.println ("maximum = number");
}
System.out.println((total/count) + " " + maximum);
scnr.close();
}
}
}

User Vinhent
by
7.9k points

1 Answer

3 votes

Final answer:

Here is an updated version of your code that fixes the errors and produces the correct output.

Step-by-step explanation:

The code provided has a few errors. Here is an updated version of your code:

import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int total = 0;
int count = 0;
int maximum = 0;
int number;
while (true) {
number = scnr.nextInt();
if (number < 0) {
break;
}
if (count == 0) {
maximum = number;
}
if (number > maximum) {
maximum = number;
}
total += number;
count++;
}

System.out.println((total/count) + " " + maximum);

scnr.close();
}
}

User Dominik Kunicki
by
7.7k points