Lab Description:
This is a program that will allow the user to enter numbers to compute several statistics. First, it show a menu that will ask the user to (1) enter a number or (2) exit. They can enter as many number as they wished positive or negatives. When the user is finish entering the number it will show:
- First number entered
- Last number entered
- Count of number entered
- Lowest two numbers
- Highest two numbers
- Total Sum
- Average
–>
Code:
import java.util.Scanner; public class number { //David Chen //CET3640 LAB 1 public static void main(String[] args){ Scanner input = new Scanner( System.in ); int InputNumber =0, a, count = 0; double firstnum = 0,lastnum = 0, min = 0, max = 0, avg = 0,sum = 0, max2 = 0, min2 = 0; System.out.println("Press 1 to enter number"); System.out.println("Press 2 to exit the program"); a = input.nextInt(); if(a == 1){ while (a ==1 ){ System.out.print("Enter A Number,to exit press Ctrl + Z: "); InputNumber = input.nextInt(); sum += InputNumber; count++; lastnum = InputNumber; if (InputNumber > max){ max2 = max; max = InputNumber; } else if ( InputNumber > max2){ max2 = InputNumber; } if (InputNumber < min) { min2 = min; min = InputNumber; } else if (InputNumber < min2){ min2 = InputNumber; } if ( count ==1 ){ max = InputNumber; min = InputNumber; firstnum = InputNumber; } else System.out.println("First Number: " +firstnum); System.out.println("Last Number: " +lastnum); System.out.println("Maximum: " +max); System.out.println("Second Maximum: " +max2); System.out.println("Minimum: " +min); System.out.println("Second Minium: " +min2); System.out.println("Average: " +sum/count); System.out.println("Total Number Enter: " +count); System.out.println("Sum: " +sum); } } else System.out.println("The program will now exit"); }} Screenshot: