Lab #1

Lab Description:
In this program we import the scanner command that allow the user to input any number(s) and display the first, last, count, the two highest and lowest numbers as well as the sum and average of the numbers that the user inputted.

import java.util.Scanner;
public class NumStats
{
    public static void main(String[] args) 
    {
    	Scanner scan = new Scanner(System.in);
        int first = 0;
        int last = 0;
        int low1 = 0;
        int low2 = 0;
        int high1 = 0;
        int high2 = 0;
        int count = 0;
        int num0;
        int num1;
        int sum = 0;
        int avg;

        do{
            System.out.println("Enter 1 to input a number");
            System.out.println("Enter 2 to exit program");
            num0 = scan.nextInt();

            if (num0 !=1 && num0 !=2)
        		System.out.println("Please enter 1 or 2 to continue:\n");
            if(num0 == 1)
            {
                System.out.println("Enter a number:");
                num1 = scan.nextInt();

                if(count == 0)
                {
                    first = num1;
                    high1 = num1;
                    high2 = num1;
                    low1 = num1;
                    low2 = num1;
                }
                else if(count == 1)
                {
                    if(num1 < high1)
	                    high2 = num1;
                    if(num1 > low1
                    	low2 = num1;
                }

                if(num1 > high1)
                {
                	high2 = high1;
                	high1 = num1;
                }
                else if(num1 > high2)
                    high2 = num1;

                if(num1 < low1)
                {
                	low2 = low1;
                    low1 = num1;
                }
                else if(num1 < low2)
                    low2 = num1;        

                sum += num1;
                last = num1;
                count++;
            	}
            	else if(num0 == 2)
            	num0= 'x';

        }while(num0 != 'x');

        avg = sum / count;

        System.out.printf("\nFirst number entered is: %d\n", first);
        System.out.printf("Last number entered is: %d\n", last);
        System.out.printf("Count of number is: %d\n", count);
        System.out.printf("Lowest two numbers are: %d and %d\n", low1, low2);
        System.out.printf("Highest two numbers are: %d and %d\n", high1, high2);
        System.out.printf("Sum is: %d\n", sum);
        System.out.printf("Average is: %d\n", avg);

        System.out.println("\n'End of program'");
        scan.close();
    }

}

Screenshot:
Lab#1

Leave a Reply

Your email address will not be published. Required fields are marked *