\Java
NestedLoops.java
//******************************************************************** // // Classname.java // //******************************************************************** // // Program Description and Design Overview: // a program that gets the average of 'count' number of values entered // // Input (read) a student id number (an integer value.) // Loop while the id number is not zero // Read a student name as a String. // Note, the Keyboard class method readString reads all characters -- INCLUDING blanks -- up until // the end of the line. // Read a count (an integer number) // Loop 'count' times: // each time through the loop read in an integer value and add that value to a sum variable // Print out the id number and name string on one line // Print out the count and average (as a double with two decimal places precision) on the next line // There should be no prompt messages printed and when output is printed your program should only // print the variable values separated by whitespace, i.e., no labels such as "count = " or "average = ". // // Input Requirements: // 1. student ID number (integer) // 2. name (string) // 3. count (integer) // 4. 'count' amount of numbers (integer) // // Output Requirements: // 1. student ID number and name on the same line // 2. 'count' and average of numbers inputed after count // // Program Preconditions: // 1. input comes from a file // //******************************************************************** import cs1.Keyboard; import java.text.DecimalFormat; public class NestedLoops { //----------------------------------------------------------------- // a program that gets the average of 'count' number of values entered //----------------------------------------------------------------- public static void main (String[] args) { int IdNum, // student ID number count, // number for how many numbers to get average of sum = 0; //total for amount of numbers enterd // Format number to two decimal places DecimalFormat fmt = new DecimalFormat ("0.##"); // read student ID number IdNum = Keyboard.readInt(); // loop while sentinel value doesn't equal 0 while (IdNum != 0) { // reads name String name = Keyboard.readString(); // reads count count = Keyboard.readInt(); // loop 'count' times and increment x by 1 for (int x = 0; count > x; x++) { // read 'numbers' int numbers = Keyboard.readInt(); // add value of numbers to sum and store in sum sum += numbers; } // find average of all the numbers double average = (double)sum / (double)count; // print ID number and name System.out.println (IdNum + " " + name); // print 'count' and average System.out.println (count + " " + fmt.format(average)); // read ID number (give chance to enter 0 to exit the loop and stop the program) IdNum = Keyboard.readInt(); } } //end main } // end class NestedLoops