Lab 1

Lab Description:

In lab 1 we are trying to create a program that ask the user the input as much numbers as they want and doing some basic statistics base on the numbers entered. What i did is using the while loop (with a Boolean condition) to ask user to input number and at the end ask the user if they want to continue entering number, if they said yes, then set the condition to true and the while loop will keep going. Otherwise set the condition to false and it will exit the loop and start doing the calculation and at the end output all the result.

Code:

//Create By Kawa Tsang
import java.util.Scanner;

public class Lab01_1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice;
        int counter=1;
        double check;
        double firstNum = 0;
        double lastNum = 0;
        double numLow1 = 0,numLow2 = 0, numHigh1 = 0, numHigh2 = 0;
        double sum=0;
        double average = 0;

        System.out.println("Welcome to My program 1, Please enter your chioce, (1) to start number statistics, (2) to exit."); //ask user what they want
        choice = input.nextInt();

        switch (choice) {
            case 1: { 
                boolean again = true;
                while (again){

                    System.out.println("Please enter the " + counter + " number: ");
                    check = input.nextDouble();

                    sum = sum + check; 

                    if (numLow1>check){
                        numLow2 = numLow1;
                        numLow1 = check;
                    }
                    else if (numLow2>check && numLow1 != check && numLow2 != numLow1)
                            numLow2 = check;
                    else if (numLow2==numLow1)
                        numLow2 = check;

                    //Find Highest 2 numbers
                    if (numHigh2<check){
                        numHigh1 = numHigh2;
                        numHigh2 = check;
                    }
                    else if (numHigh1<check && numHigh2 != check && numHigh1 != numHigh2)
                            numHigh1 = check;
                    else if (numHigh1==numHigh2)
                        numHigh1 = check;

                    //first number
                    if (counter == 1){ 
                        firstNum = check;
                        numLow1 = check;
                        numLow2 = check;
                        numHigh1 = check;
                        numHigh2 = check;
                    }

                    System.out.println("Coutinue entering?(Y to continue, any other key to stop entering number)");
                    char in = input.next().charAt(0);

                    if(in == 'y' || in == 'Y' ){
                        again = true;
                        counter++;
                    }
                    else {
                        lastNum = check;
                        again = false;
                    }
                }
                average = sum / counter; // calculate the average

                System.out.println("The first number entered is " + firstNum + ".");       //output all the statistics
                System.out.println("The last number entered is " + lastNum + ".");
                System.out.println("The count of number entered is " + counter + ".");
                System.out.println("The lowest two numbers are " + numLow1 + " and " + numLow2 +"." );
                System.out.println("The highest two numbers are " + numHigh1 + " and " + numHigh2 +"." );
                System.out.println("The total sum is " + sum + ".");
                System.out.println("The average is " + average + ".");

                input.close();
                return;            

            }

            case 2: {   //Exit
                input.close();
                return;
            }

            default: {  //unexpected input
                System.out.println("Invalid choice, Program exit");
                input.close();    
                return;
            }
        }
    }
}

Screenshot

Screen Shot 2013-09-30 at 3.02.20 PMScreen Shot 2013-09-30 at 2.59.27 PM

Leave a Reply

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