Java Write a program that asks the user to enter any number

Java

Write a program that asks the user to enter any number of test scores and calculate their averages. After each test score is entered, validate that the score is between 0 to 100. [Note: this includes 0 and 100 as valid scores]. As you can enter any number of scores, there needs to be an approach to end the entry of scores. In order to exit the entry of scores, you need to enter a -1 value. Consider this as a sentinel for the program to conclude. As a part of your algorithm, determine the lowest and highest scores that were entered. At the conclusion of the program, if one or more scores have been entered, the program displays: - the number of test scores that were entered - the lowest score - the highest score - the average of the scores If no scores were entered, display the message that “No test grade scores were entered”.

Solution

CalAvgMinMax.java

import java.util.ArrayList;
import java.util.Scanner;

public class CalAvgMinMax {

   public static void main(String[] args) {
   //Declaring variables.
      int value,sum=0,count=0,min=0,max=0;
              double average;

       //Scanner class object is used to read the value entered by the user
Scanner sc=new Scanner(System.in);

//Creating the ArrayList Object
ArrayList<Integer> al=new ArrayList<Integer>();

    while(true)
    {
    //Getting the number from the user.
    System.out.print(\"Enter a Number :\");
    value=sc.nextInt();
      
    /* If the entered value is -1
    * Program will exit.
    */
    if(value==-1)
    {
    System.out.println(\":: No test grade scores were entered ::\");  
    System.out.println(\":: Program Exit ::\");  
    System.exit(0);
    }
    else if(value<0 || value>100)
    {
        System.out.println(\"** Invalid Value.Number must be between 0 and 100( Inclusive) **\");
        continue;
    }
    else
    break;
    }
   
    //Adding the value to the ArrayList
    al.add(value);
   
    //Assignging the value to min and max variables
    min=value;
    max=value;
   

        while(true)
        {
            /* For every valid input(Entered Number other than zero)
            * increment the count value by 1
            */
            count++;
           
            //Calculating the sum
            sum+=value;
            while(true)
            {
            //Getting the number entered by the user
            System.out.print(\"Enter a Number :\");
            value=sc.nextInt();
           
            if(value==-1)
            {
               //Calculating the average
               average=(double)sum/count;
              
               //Displaying all the numbers user entered.
               System.out.println(\"The Numbers you entered are :\");
               for(Integer i:al)
               {
                   System.out.println(i+\" \");
               }
              
               //Displaying the average
               System.out.printf(\"Average %.2f:\",average);
              
               //Displaying the maximum number
               System.out.println(\"\ Max :\"+max);
              
               //Displaying the minimum number
               System.out.println(\"Min :\"+min);
              
                System.out.println(\":: Program Exit ::\");  
                System.exit(0);
            }
            else if(value<0 || value>100)
           {
               System.out.println(\"** Invalid Value.Number must be between 0 and 100( Inclusive) **\");
               continue;
           }
           else
           break;
           }
           
            //Adding value to the ArrayList
            al.add(value);
           
            //calculating the maximum and minimum values
            if(value>max)
        max=value;
        if(value<min)
        min=value;
       
                   
        }

   }

}

___________________________________

output1:

Enter a Number :-1
:: No test grade scores were entered ::
:: Program Exit ::

___________________________________
Output2:

Enter a Number :56
Enter a Number :78
Enter a Number :90
Enter a Number :54
Enter a Number :34
Enter a Number :22
Enter a Number :11
Enter a Number :79
Enter a Number :99
Enter a Number :-65
** Invalid Value.Number must be between 0 and 100( Inclusive) **
Enter a Number :65
Enter a Number :-1
The Numbers you entered are :
56
78
90
54
34
22
11
79
99
65
Average :58.8
Max :99
Min :11
:: Program Exit ::

_____________Thank You

Java Write a program that asks the user to enter any number of test scores and calculate their averages. After each test score is entered, validate that the sco
Java Write a program that asks the user to enter any number of test scores and calculate their averages. After each test score is entered, validate that the sco
Java Write a program that asks the user to enter any number of test scores and calculate their averages. After each test score is entered, validate that the sco

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site