Lab Description:
In this lab project we take create a code that will take in an infinite amount of numbers until you enter 0. Then it will take those numbers and present the following. The first number entered, the last number entered, the amount of numbers entered, the highest number, the lowest number, the sum of all the numbers, and the average of all the numbers. This project took on a more deeper use of java.
Code:
import java.util.Scanner; public class numbers { private static Scanner input; public static void main(String[] args) { input = new Scanner(System.in); int first= 0; int last= 0; //The max and min. int maxNum = Integer.MIN_VALUE; // Maximum number. int minNum = Integer.MAX_VALUE; // Minimum number. int count = 0; int totalNum = 0; // Loop until 0 is entered. System.out.println("Please enter your numbers or 0 to quit:"); while (input.hasNextInt()) { int number = input.nextInt(); if (number == 0) { break; } if (number > maxNum) { maxNum = number; } if (number < minNum) { minNum = number; } int numbers = string('1'); first = numbers; last = number; count++; totalNum += number; } if (count > 0) { // Display double average = totalNum / count; System.out.println("The first number: " + first); System.out.println("The last number: " + last); System.out.println("Number of values: " + count); System.out.println("Maximum : " + maxNum); System.out.println("Minimum : " + minNum); System.out.println("Sum of all values: " + totalNum); System.out.println("Average : " + average); } else { System.out.println("No data is entered"); } } private static int string(char c) { // TODO Auto-generated method stub return 0; } }
Screenshots: