\Java
SentinelLoop.java
//********************************************************************
//
// SentinelLoop.java
//
//********************************************************************
import cs1.Keyboard;
public class SentinelLoop
{
//-----------------------------------------------------------------
// A comment that must be replaced by comment(s) describing the // program's main method.
//-----------------------------------------------------------------
public static void main (String[] args)
{
// Program SentinelLoop prompts the user for an integer number,
// reads the number, and adds it into a sum.
// The loop continues until the user types a negative number.
// Then the sum is printed.
// ============================================================
// The following comment statements describe the program design
// steps necessary to write this program
// BUT THEY ARE NOT IN THE CORRECT ORDER!!!
// Do the following:
// 1. Figure out the correct design solution for this program using the
// steps in each of the comments below. It's best to write this
// down on paper rather than starting to edit your Java source file.
// 2. Using the JCreator editor copy and paste the Java comment lines
// below into their proper order in your source file.
// 3. After each comment add the Java statement (or statements) that
// does what each comment says
// 4. Compile, execute, and test your program.
// ==> Some of these design steps may be used more than once.
// variable for acculumating the sum (a declaration statement)
// variable for storing the user's input value (a declaration statement)
int sum,value;
sum = 0; // initialize sum to zero
// Prompt the user by printing a message such as,
// "Enter an integer value; press return."
// "To signal end of the input, enter a negative value."
System.out.println ("Enter an integer value; press return. \n To signal end of the input, enter a negative value.");
// Read the user's input value
value = Keyboard.readInt();
// main loop
while ( value >= 0 ) // (fill in the missing Loop Termination Condition, LTC)
{
// Add the input value to the running sum
sum += value;
// Prompt the user by printing a message such as,
// "Enter an integer value; press return."
// "To signal end of the input, enter a negative value."
System.out.println ("Enter an integer value; press return.\nTo signal end of the input, enter a negative value.");
// Read the user's input value
value = Keyboard.readInt();
}
// Print the value of sum (including a label such as, "The sum is ")
System.out.println ("The sum is " + sum);
} //end main
} // end class SentinelLoop
//The program printed the same line as many times as numbers entered separated by a space
//because it ran the loop that many times and reentered the value of the numbers into the
//integer variable value