Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. /**
  3. *
  4. * @author Christian Williams
  5. * CSC 120 Lab5
  6. * Fall 2015
  7. *
  8. */
  9. public class Lab5 {
  10.  
  11. public static void main(String[] args) {
  12. // Declare and initialize variables.
  13. final String OUTPUT_TITLE = "Lab5 by Christian Williams";
  14. int maxFoundSoFar = -1;
  15. int minFoundSoFar = 101;
  16. int validInputs = 0;
  17. int invalidInputs = 0;
  18. int sumOfTestScores = 0;
  19. int userInt = 0;
  20. double averageTestScore;
  21. String summary = "";
  22. // Do -- we loop until he enters a -1.
  23. do {
  24. // userString = get string from user with prompt about 0 to 100
  25. // and use of -1
  26. String userString = JOptionPane.showInputDialog(null, "Enter a test score between 0 to 100"
  27. + " or enter -1 to exit" + "Test Grades", OUTPUT_TITLE, JOptionPane.NO_OPTION);
  28. // userInt = convert users String to int for computations
  29. userInt = Integer.parseInt(userString);
  30. // If userInt is not -1
  31. if (userInt != -1){
  32. // If userInt is out of legal range
  33. if (userInt < 0 || userInt > 100){
  34. // Increment the invalid-inputs counter.
  35. ++invalidInputs;
  36. // Give user a bad-input message (we will ignore his input)
  37. JOptionPane.showMessageDialog(null, "Bad input. Enter the test score again.",
  38. OUTPUT_TITLE, JOptionPane.NO_OPTION);
  39. } // end inner if statement
  40. // Else it is good input so:
  41. else {
  42. // Add userInt to our sum.
  43. sumOfTestScores += userInt;
  44. // Increment counter of valid-inputs
  45. ++validInputs;
  46. // If userInt < minFoundSoFar
  47. if (userInt < minFoundSoFar){
  48. // minFoundSoFar = userInt
  49. minFoundSoFar = userInt;
  50. } // end if statement
  51. // If userInt > maxFoundSoFar
  52. else if (userInt > maxFoundSoFar){
  53. // maxFoundSoFar = userInt
  54. maxFoundSoFar = userInt;
  55. }// end else if statement
  56. // Else do nothing
  57. else {
  58. }// end else statement
  59. }// end else statement
  60. }// end if statement
  61. } // end do statement
  62. // While userInt is not -1
  63. while (userInt != -1);
  64. // end do-while statement
  65. // If valid-inputs is greater than 0
  66. if (validInputs > 0){
  67. // Compute average [may need type casting to avoid int division!]
  68. averageTestScore = Math.round((double)sumOfTestScores / (double)validInputs);
  69. // Give a summary report
  70. summary += "Total test scores entered: " + validInputs + "\n";
  71. summary += "Average of all test scores: " + averageTestScore + "\n";
  72. summary += "Highest grade entered: " + maxFoundSoFar + "\n";
  73. summary += "Lowest grade entered: " + minFoundSoFar + "\n";
  74. summary += "Number of invalid entries: " + invalidInputs + "\n";
  75. JOptionPane.showMessageDialog(null, summary, OUTPUT_TITLE, JOptionPane.NO_OPTION);
  76. }// end if statement
  77. // Else
  78. else {
  79. // Tell user that no valid were entered hence no summary report.
  80. summary += "You did not enter any valid test scores.";
  81. JOptionPane.showMessageDialog(null, summary, OUTPUT_TITLE, JOptionPane.NO_OPTION);
  82. }// end else statement
  83.  
  84. }// end main method
  85.  
  86. }// end lab5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement