Exercise
Statistics
Objetive
Write a java program to calculate various basic statistical operations: it will accept numbers from the user and display their sum, average, minimum and maximum, as in the following example:
Number? 5
Total=5 Count=1 Average=5 Max=5 Min=5
Number? 2
Total=7 Count=2 Average=3.5 Max=5 Min=2
Number? 0
Goodbye!
(As seen in this example, the program will end when the user enters 0)
Example Code
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num;
int total = 0, amount = 0;
int maximum, minimum;
System.out.print("number? ");
num = Integer.parseInt(new Scanner(System.in).nextLine());
maximum = num;
minimum = num;
while (num != 0)
{
amount++;
total += num;
if (num > maximum)
{
maximum = num;
}
if (num < minimum)
{
minimum = num;
}
System.out.printf("Total=%1$s Amount=%2$s Average=%3$s maximum=%4$s minimum=%5$s" + "\r\n", total, amount, total / amount, maximum, minimum);
System.out.print("number? ");
num = Integer.parseInt(new Scanner(System.in).nextLine());
}
System.out.println("Bye!");
}
}