Đang chuẩn bị liên kết để tải về tài liệu:
Beginning Programming with Java for Dummies 2nd phần 6

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

: Vâng, tôi có thể hoặc không thể cho bạn biết câu trả lời cho câu hỏi của bạn. Tôi: Vâng, đó là nó? Bạn có thể RoboJeeves: Vâng, tôi có thể. Tôi: Sau đó, Sau đó, tôi kiểm tra tập tin đầu ra của chương trình. Tập tin đầu ra (hiển thị trong hình 13-8) chứa chỉ có hai chữ cái. | 188 Part III Controlling the Flow__ You can t use a condition as a case value either You can t do this switch age case age 12 . .etc. To break or not to break In every Java programmer s life a time comes when he or she forgets to use break statements. At first the resulting output is confusing but then the programmer remembers fall-through. The term fall-through describes what happens when you end a case without a break statement. What happens is that execution of the code falls right through to the next case in line. Execution keeps falling through until you eventually reach a break statement or the end of the entire switch statement. If you don t believe me just look at Listing 11-2. This listing s code has a switch statement gone bad Listing 11-2 Please Gimme a Break This isn t good code. The programmer forgot some of the break statements. import java.util.Scanner import java.util.Random import static java.lang.System.out class BadBreaks public static void main String args Scanner myScanner new Scanner System.in Random myRandom new Random int randomNumber out.print Type your question my child myScanner.nextLine randomNumber myRandom.nextInt 10 1 switch randomNumber case 1 out.println Yes. Isn t it obvious case 2 out.println No and don t ask again. Chapter 11 How to Flick a Virtual Switch 189 case 3 out.printCYessir yessir out.printlnC Three bags full. case 4 out.printCWhat part of no out.printlnC don t you understand break case 5 out.printlnCNo chance Lance. case 6 out.printlnCSure whatever. case 7 out.printCYes but only if out.printlnC you re nice to me. case 8 out.printlnCYes as if I care . case 9 out.printCNo not until out.printlnC Cromwell seizes Dover. case 10 out.printCNo not until out.printlnC Nell squeezes Rover. default out.printCYou think you have out.printc problems out.printc My random number out.printlnC generator is broken out.printlnCGoodbye I ve put two runs of this code in Figure 11-3. In