Controlling a Loop with a Confirmation Dialog

Một phần của tài liệu Java TM programming (Trang 197 - 200)

You can use a confirmation dialog to prompt the user to confirm whether to continue or exit a loop.

A sentinel-controlled loop can be implemented using a confirmation dialog. The answers Yes orNocontinue or terminate the loop. The template of the loop may look as follows:

int option = JOptionPane.YES_OPTION;

while (option == JOptionPane.YES_OPTION) { System.out.println("continue loop");

option = JOptionPane.showConfirmDialog(null, "Continue?");

}

Listing 4.15 rewrites Listing 4.5, SentinelValue.java, using a confirmation dialog box. A sample run is shown in Figure 4.4.

LISTING4.15 SentinelValueUsingConfirmationDialog.java

1 import javax.swing.JOptionPane;

2

3 public class SentinelValueUsingConfirmationDialog { 4 public static void main(String[] args) {

5 int sum = 0;

divisor <= number / 2 && isPrime;

subproblem

Key Point confirmation dialog

Key Terms 165

6

7 // Keep reading data until the user answers No 8 int option = JOptionPane.YES_OPTION;

9 while (option == JOptionPane.YES_OPTION) { 10 // Read the next data

11 String dataString = JOptionPane.showInputDialog(

12 "Enter an integer: ");

13 int data = Integer.parseInt(dataString);

14

15 sum += data;

16

17 option = JOptionPane.showConfirmDialog(null, "Continue?");

18 } 19

20 JOptionPane.showMessageDialog(null, "The sum is " + sum);

21 } 22 }

(a) (b)

(c) (d) (e)

FIGURE4.4 The user enters 3 in (a), clicks Yes in (b), enters 5 in (c), clicks No in (d), and the result is shown in (e).

confirmation option check option input dialog

confirmation dialog

message dialog

The program displays an input dialog to prompt the user to enter an integer (line 11) and adds it to sum(line 15). Line 17 displays a confirmation dialog to let the user decide whether to continue the input. If the user clicks Yes, the loop continues; otherwise, the loop exits.

Finally, the program displays the result in a message dialog box (line 20).

TheshowConfirmDialogmethod (line 17) returns an integer JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION, if the user clicks Yes, No, or Cancel. The return value is assigned to the variable option (line 17). If this value is JOptionPane.YES_OPTION, the loop continues (line 9).

K EY T ERMS

breakstatement 159 continuestatement 159 do-whileloop 144 forloop 147 infinite loop 136 input redirection 143 iteration 134 loop 134

loop body 134 nested loop 152 off-by-one error 136 output redirection 143 posttest loop 150 pretest loop 150 sentinel value 141 whileloop 134

C HAPTER S UMMARY

1. There are three types of repetition statements: the whileloop, the do-while loop, and the forloop.

2. The part of the loop that contains the statements to be repeated is called the loop body.

3. A one-time execution of a loop body is referred to as an iteration of the loop.

4. Aninfinite loopis a loop statement that executes infinitely.

5. In designing loops, you need to consider both the loop control structureand the loop body.

6. Thewhileloop checks the loop-continuation-conditionfirst. If the condition istrue, the loop body is executed; if it is false, the loop terminates.

7. Thedo-whileloop is similar to the whileloop, except that the do-whileloop exe- cutes the loop body first and then checks the loop-continuation-conditionto decide whether to continue or to terminate.

8. Thewhileloop and the do-whileloop often are used when the number of repetitions is not predetermined.

9. Asentinel valueis a special value that signifies the end of the loop.

10. Theforloop generally is used to execute a loop body a predictable number of times;

this number is not determined by the loop body.

11. Theforloop control has three parts. The first part is an initial action that often initial- izes a control variable. The second part, the loop-continuation-condition, determines whether the loop body is to be executed. The third part is executed after each iteration and is often used to adjust the control variable. Usually, the loop control variables are initialized and changed in the control structure.

12. Thewhileloop and forloop are called pretest loopsbecause the continuation condi- tion is checked before the loop body is executed.

13. Thedo-whileloop is called a posttest loopbecause the condition is checked after the loop body is executed.

14. Two keywords, breakandcontinue, can be used in a loop.

15. Thebreakkeyword immediately ends the innermost loop, which contains the break.

16. Thecontinuekeyword only ends the current iteration.

T EST Q UESTIONS

Do the test questions for this chapter online at www.cs.armstrong.edu/liang/intro9e/test.html.

P ROGRAMMING E XERCISES

Pedagogical Note

Read each problem several times until you understand it. Think how to solve the prob- lem before starting to write code. Translate your logic into a program.

A problem often can be solved in many different ways. Students are encouraged to explore various solutions.

read and think before coding

explore solutions

Một phần của tài liệu Java TM programming (Trang 197 - 200)

Tải bản đầy đủ (PDF)

(1.947 trang)