Using flag bits to control loops
The usual techniques for controlling loops are mentioned earlier: counter-controlled loops. Another common technique for controlling loops is to assign a special value when reading and processing the value of a collection. This particular input value also becomes the flag value (Sentinel values), Used to indicate the end of the loop. If a loop uses a flag value to control its execution, he is called the loop of the flag-bit control (sentinel-controlled loop).
In loop control, do not use floating-point values to compare values for equality. Because floating-point values are approximate values of certain values, using them can result in inaccurate loops and inaccurate results.
Input and output redirection
Input redirection refers to reading input from a file, not from the keyboard.
Output redirection refers to sending a transmission to a file, not to a screen.
Sentinevalue class code at the end of the text, the F:\workspace\Welcome\bin directory under the new Input.txt file, the contents are as follows:
PackageWelcome;ImportJava.util.Scanner;/** used to read and calculate the sum of an indeterminate number of integers, input 0 indicates the end of the input*/ Public classSentinelvalue { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Enter an integer that will exit if you enter 0:"); intdata =In.nextint (); intsum = 0; while(Data! = 0) {sum= Sum +data; System.out.print ("Enter an integer that will exit if you enter 0:"); Data=In.nextint (); } System.out.println ("The sum is" +sum); }}
Java Learning notes---using flag bits to control loops