Colindapieman

TestScoreAppCSE

Feb 9th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.NumberFormat;
  5.  
  6. public class TestScoreApp
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         // display operational messages
  11.         System.out.println("Please enter test scores that range from 0 to 100.");
  12.         System.out.println("To end the program enter 999.");
  13.         System.out.println();  // print a blank line
  14.  
  15.         // initialize tally variables and create a Scanner object
  16.         int scoreTotal = 0;
  17.         int scoreCount = 0;
  18.         int testScore = 0;
  19.         Scanner sc = new Scanner(System.in);
  20.         //initialize minimumScore and maximumScore integers
  21.         int minimumScore = 100;
  22.         int maximumScore = 0;
  23.            
  24.         // get a series of test scores from the user
  25.         while (testScore != 999)
  26.         {
  27.             // get the input from the user
  28.             System.out.print("Enter score: ");
  29.             testScore = sc.nextInt();
  30.  
  31.             // accumulate score count and score total
  32.             if (testScore <= 100)
  33.             {
  34.                 scoreCount += 1;
  35.                 scoreTotal += testScore;
  36.                //accumulate the minimum and maximum scores
  37.                 minimumScore = Math.min(testScore,minimumScore);
  38.                 maximumScore = Math.max(testScore,maximumScore);
  39.             }
  40.             else if (testScore != 999)
  41.                 System.out.println("Invalid entry, not counted");
  42.         }
  43.         // calculate the average score and display the results
  44.        
  45.         BigDecimal scoreTotalBD = new BigDecimal(scoreTotal);
  46.         BigDecimal scoreCountBD = new BigDecimal(scoreCount);
  47.         BigDecimal averageScoreBD = scoreTotalBD.divide( scoreCountBD,1,  RoundingMode.HALF_UP);
  48.  
  49.         //create an instance of NumberFormat so you can use its static methods.
  50.         //NumberFormat nf = NumberFormat.getInstance();
  51.         //nf.setMaximumFractionDigits(1);
  52.         // display the results
  53.         String message = "\n" +
  54.                          "Score count:   " + scoreCount + "\n"
  55.                        + "Score total:   " + scoreTotal + "\n"
  56.                        //+ "Average score: " + nf.format(averageScore) + "\n"
  57.                        + "AverageScore:   " + averageScoreBD + "\n"
  58.                        + "Minimum Value: " + minimumScore + "\n"
  59.                        + "Maximum Value: " + maximumScore + "\n";
  60.         System.out.println(message);
  61.         sc.close();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment