Lab 1
Lab 1
Overview:
In this la
Lab 1 procedures:
Create a program that will prompt the user to enter numbers to compute several statistics. First, display a menu that will ask the user to (1) enter a number or (2) exit. The user should be able to enter as many numbers as wished (positives or negatives). When the user finishes the following statistics should be displayed:
- First number entered
- Last number entered
- Count of number entered
- Lowest two numbers
- Highest two numbers
- Total Sum
- Average
Overview:
In this lab I was able to complete the tasked asked from the user with a simple number of loops. In addition, I added a way for the program to exit if an invalid option is enters.
Code:
package Lab1; import java.util.*; public class Lab1 { public static void main(String[] args) { int count = 0, firstNum = 0, lastNum = 0, lowNum = 32767, hiNum = 0, sum = 0, avg = 0, choice = 1; int lowNum2 = lowNum; int hiNum2 = hiNum; while (choice == 1) { @SuppressWarnings("resource") Scanner keyboard = new Scanner(System.in); System.out.println("What would you like to do: \n1) add a number \n2) exit " ); choice = keyboard.nextInt(); if(choice > 2 || choice < 1) { System.out.println("Incorrect input system will exit!"); } if(choice == 2) { System.out.println("Thank you have a nice day!"); } if(choice == 1) { System.out.println("Please enter a number"); int input = keyboard.nextInt(); if(count == 0) { firstNum = input; } lastNum = input; if(lowNum > input) { lowNum = input; } if(lowNum < lowNum2) { lowNum2 = lowNum; } if(hiNum < input) { hiNum2 = hiNum; hiNum = input; } sum = sum + input; count++; avg = sum/count; } } System.out.println("The first number = " + firstNum); System.out.println("The last number = " + lastNum); System.out.println("The first lowest number = " + lowNum); System.out.println("The second lowest number = " + lowNum2); System.out.println("The first highest number = " + hiNum); System.out.println("The second highest number = " + hiNum2); System.out.println("The total sum = " + sum); System.out.println("The avrage of all numbers = " + avg); } }
Screenshots:
Leave a Reply