Beginning Programming with Java for Dummies 2nd phần 6

: 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 import import static class BadBreaks public static void main String args Scanner myScanner new Scanner Random myRandom new Random int randomNumber Type your question my child randomNumber 10 1 switch randomNumber case 1 Yes. Isn t it obvious case 2 No and don t ask again. Chapter 11 How to Flick a Virtual Switch 189 case 3 yessir Three bags full. case 4 part of no don t you understand break case 5 chance Lance. case 6 whatever. case 7 but only if you re nice to me. case 8 as if I care . case 9 not until Cromwell seizes Dover. case 10 not until Nell squeezes Rover. default think you have problems My random number generator is broken I ve put two runs of this code in Figure 11-3. In

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
14    117    4    03-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.