Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.NumberFormat;
- public class TestScoreApp
- {
- public static void main(String[] args)
- {
- // display operational messages
- System.out.println("Please enter test scores that range from 0 to 100.");
- System.out.println("To end the program enter 999.");
- System.out.println(); // print a blank line
- // initialize tally variables and create a Scanner object
- int scoreTotal = 0;
- int scoreCount = 0;
- int testScore = 0;
- Scanner sc = new Scanner(System.in);
- //initialize minimumScore and maximumScore integers
- int minimumScore = 100;
- int maximumScore = 0;
- // get a series of test scores from the user
- while (testScore != 999)
- {
- // get the input from the user
- System.out.print("Enter score: ");
- testScore = sc.nextInt();
- // accumulate score count and score total
- if (testScore <= 100)
- {
- scoreCount += 1;
- scoreTotal += testScore;
- //accumulate the minimum and maximum scores
- minimumScore = Math.min(testScore,minimumScore);
- maximumScore = Math.max(testScore,maximumScore);
- }
- else if (testScore != 999)
- System.out.println("Invalid entry, not counted");
- }
- // calculate the average score and display the results
- BigDecimal scoreTotalBD = new BigDecimal(scoreTotal);
- BigDecimal scoreCountBD = new BigDecimal(scoreCount);
- BigDecimal averageScoreBD = scoreTotalBD.divide( scoreCountBD,1, RoundingMode.HALF_UP);
- //create an instance of NumberFormat so you can use its static methods.
- //NumberFormat nf = NumberFormat.getInstance();
- //nf.setMaximumFractionDigits(1);
- // display the results
- String message = "\n" +
- "Score count: " + scoreCount + "\n"
- + "Score total: " + scoreTotal + "\n"
- //+ "Average score: " + nf.format(averageScore) + "\n"
- + "AverageScore: " + averageScoreBD + "\n"
- + "Minimum Value: " + minimumScore + "\n"
- + "Maximum Value: " + maximumScore + "\n";
- System.out.println(message);
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment