TestScores Critique
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
/** Inputs 'n' test scores, then outputs the lowest (minimum), highest test score (maximum), average test score(average). Robert Studenic rs.nibbles at gmail.com */ import java.util.Scanner; public class TestScores { // Input # of tests and test scores. int testNum = input.nextInt(); int i = 1; double max = 0; double min = 0; double avg = 0; while( i <= testNum){ double test = input.nextDouble(); // Test 'test' to see if it is a valid input. if(test < 0){ return; } // Test variables to check if this is initial while run. if(i == 1){ max = test; min = test; avg = test; } // Testing 'test' if it is lower than the current value for min. if(test < min) min = test; // Testing 'test' if it is higher than the current value for max. if(test > max) max = test; // Adding 'test' to the value of avg. if(i == 1); else avg += test; ++i; } // Calculating average. avg = avg / testNum; // Output min, max, and avg. } }
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.


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 justif (i != 1). That way you don't need an else clause.