TestScores Critique

2 replies [Last post]
nibbles's picture
User offline. Last seen 1 week 5 days ago. Offline
Joined: 05/14/2009
Posts:
Groups: None

Just curious if this could be written in a better way from an I:CS perspective. Personal disclaimer: Do not copy this code for the means of being lazy in Intro to Computer Science

  1. /**
  2.   Inputs 'n' test scores, then outputs the lowest
  3.   (minimum), highest test score (maximum), average
  4.   test score(average).
  5.  
  6.   Robert Studenic
  7.   rs.nibbles at gmail.com
  8.  */
  9.  
  10. import java.util.Scanner;
  11.  
  12. public class TestScores {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. Scanner input = new Scanner(System.in);
  17.  
  18. // Input # of tests and test scores.
  19. System.out.println("Input the number of tests to calculate: ");
  20. int testNum = input.nextInt();
  21. int i = 1;
  22. double max = 0;
  23. double min = 0;
  24. double avg = 0;
  25.  
  26. while( i <= testNum){
  27. System.out.println("Input score for Test " + i + " : ");
  28. double test = input.nextDouble();
  29.  
  30. // Test 'test' to see if it is a valid input.
  31. if(test < 0){
  32. System.out.println("This is not a valid input. Program exiting.");
  33. return;
  34. }
  35.  
  36. // Test variables to check if this is initial while run.
  37. if(i == 1){
  38. max = test;
  39. min = test;
  40. avg = test;
  41. }
  42.  
  43. // Testing 'test' if it is lower than the current value for min.
  44. if(test < min)
  45. min = test;
  46.  
  47. // Testing 'test' if it is higher than the current value for max.
  48. if(test > max)
  49. max = test;
  50.  
  51. // Adding 'test' to the value of avg.
  52. if(i == 1);
  53. else
  54. avg += test;
  55.  
  56. ++i;
  57.  
  58. }
  59. // Calculating average.
  60. avg = avg / testNum;
  61.  
  62. // Output min, max, and avg.
  63. System.out.printf("The minimum for the " + testNum + " tests is " + min + ".\n");
  64. System.out.printf("The average for the " + testNum + " tests is " + avg + ".\n");
  65. System.out.printf("The maximum for the " + testNum + " tests is " + max + ".\n");
  66.  
  67. }
  68. }

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tdf11's picture
User offline. Last seen 6 hours 42 min ago. Offline
Joined: 05/01/2009
Posts:
Groups: SIGCOMP, SIGHACK
Re: TestScores Critique

Nope, looks pretty good to me, as far as the structure goes. The only thing I'd change would be indentation inside your while loop. And change the if (i == 1); to just if (i != 1). That way you don't need an else clause.

wordmagekat's picture
User offline. Last seen 2 days 4 hours ago. Offline
Joined: 05/10/2009
Posts:
Groups: None
Re: TestScores Critique

Looks pretty good to me, too. I would use a for loop instead of a while, though. I have a tendency to forget to increment counters. Better if you don't have to worry about it, and it makes it a little clearer to the reader exactly how many times you're iterating.

This student organization is a registered student organization of The University of Akron. Registration shall not be construed as approval, endorsement, or sponsorship by The University of Akron of the student organization's publications, activities, purposes, actions, or positions.