Had to write the following program for an on line pre java class using, while, do-while and for loops.
Looking for a little explanation. Thanks in advance!
PS While looking for reference books is this Java or Javascript? Any suggestions for a good reference book? I get the concept, mostly, the devil is certainly in the details.
public class ExamsFor {
public static void main(String[] arguments) {
int inputNumber; // One of the exams input by the user.
int sum; // The sum of the exams.
int count; // Number of exams.
Double Avg; // The average of the exams.
/* Initialize the summation and counting variables. */
sum = 0;
count = 0;
/* Read and process the user's input. */
TextIO.put("Please enter the first exam: "); // get the first exam.
inputNumber = TextIO.getlnInt();
for (inputNumber!=0; sum += inputNubmer; count++ ) { // had the while loop below enter here, worked
TextIO.put("Please enter the next exam, or 0 to end: "); // get the next exam.
inputNumber = TextIO.getlnInt();
}
/* Display the result. */
if (count == 0) {
TextIO.putln("You didn't enter any data!");
}
else {
Avg = ((double)sum) / count;
TextIO.putln();
TextIO.putln("You entered " + count + " exams.");
TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
}
} // end main ()
} // end class ExamsFor
/* Had the following 'while loop' in place of the 'for loop'
while (inputNumber != 0) {
sum += inputNumber; // Add inputNumber to running sum.
count ++; // Count the input by adding 1 to the count.
*/