Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /**
- *
- * @author Christian Williams
- * CSC 120 Lab5
- * Fall 2015
- *
- */
- public class Lab5 {
- public static void main(String[] args) {
- // Declare and initialize variables.
- final String OUTPUT_TITLE = "Lab5 by Christian Williams";
- int maxFoundSoFar = -1;
- int minFoundSoFar = 101;
- int validInputs = 0;
- int invalidInputs = 0;
- int sumOfTestScores = 0;
- int userInt = 0;
- double averageTestScore;
- String summary = "";
- // Do -- we loop until he enters a -1.
- do {
- // userString = get string from user with prompt about 0 to 100
- // and use of -1
- String userString = JOptionPane.showInputDialog(null, "Enter a test score between 0 to 100"
- + " or enter -1 to exit" + "Test Grades", OUTPUT_TITLE, JOptionPane.NO_OPTION);
- // userInt = convert users String to int for computations
- userInt = Integer.parseInt(userString);
- // If userInt is not -1
- if (userInt != -1){
- // If userInt is out of legal range
- if (userInt < 0 || userInt > 100){
- // Increment the invalid-inputs counter.
- ++invalidInputs;
- // Give user a bad-input message (we will ignore his input)
- JOptionPane.showMessageDialog(null, "Bad input. Enter the test score again.",
- OUTPUT_TITLE, JOptionPane.NO_OPTION);
- } // end inner if statement
- // Else it is good input so:
- else {
- // Add userInt to our sum.
- sumOfTestScores += userInt;
- // Increment counter of valid-inputs
- ++validInputs;
- // If userInt < minFoundSoFar
- if (userInt < minFoundSoFar){
- // minFoundSoFar = userInt
- minFoundSoFar = userInt;
- } // end if statement
- // If userInt > maxFoundSoFar
- else if (userInt > maxFoundSoFar){
- // maxFoundSoFar = userInt
- maxFoundSoFar = userInt;
- }// end else if statement
- // Else do nothing
- else {
- }// end else statement
- }// end else statement
- }// end if statement
- } // end do statement
- // While userInt is not -1
- while (userInt != -1);
- // end do-while statement
- // If valid-inputs is greater than 0
- if (validInputs > 0){
- // Compute average [may need type casting to avoid int division!]
- averageTestScore = Math.round((double)sumOfTestScores / (double)validInputs);
- // Give a summary report
- summary += "Total test scores entered: " + validInputs + "\n";
- summary += "Average of all test scores: " + averageTestScore + "\n";
- summary += "Highest grade entered: " + maxFoundSoFar + "\n";
- summary += "Lowest grade entered: " + minFoundSoFar + "\n";
- summary += "Number of invalid entries: " + invalidInputs + "\n";
- JOptionPane.showMessageDialog(null, summary, OUTPUT_TITLE, JOptionPane.NO_OPTION);
- }// end if statement
- // Else
- else {
- // Tell user that no valid were entered hence no summary report.
- summary += "You did not enter any valid test scores.";
- JOptionPane.showMessageDialog(null, summary, OUTPUT_TITLE, JOptionPane.NO_OPTION);
- }// end else statement
- }// end main method
- }// end lab5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement