Lab Description:
A basic java program closely resembling of C++ is implemented to calculated basic math statistics. A “do while” loop with “if” statements is used to configure the statistics such as Maximum, Minimum, first number, last number etc. All the integer variables are set to zero, the object “Scanner” is used also which allows for reading data from the user.
import java.util.Scanner;public class lab2
{
public static void main(String[] args)
{int first = 0;
int last = 0;
int min = 0;
int number = 0;
int max = 0;
int sum = 0;
int numberAmount = 0; //keeps count of how many numbers are entered
int average = 0;//Create a Scanner object for keyboard input.
Scanner input = new Scanner(System.in);//Display general instructions.
System.out.println ("Enter a series of numbers.");System.out.println ("Enter 0 when finshed entering numbers.");
do {
//Get the numbers from user input.
number = input.nextInt();
if (numberAmount == 0){
first = number;
min = number;
max = number;}
sum = sum + number;
if(number != 0){
last = number;
numberAmount++; //value increases by one every time a new number is entered
}if (number > max)
{
max = number;}
if (number < min) { min = number; } } while (number != 0); average = sum / numberAmount; System.out.println("The average is: " + average); System.out.println("The largest number is: " + max); System.out.println("The Total amount of numbers you have entered is: " + numberAmount); System.out.println("The sum is: " + sum); System.out.println("The smallest number is: " + min ); System.out.println("The first number is: " + first ); System.out.println("The last number is: " + last ); } } Screenshots: