tailieunhanh - Beginning Programming with Java for Dummies 2nd phần 5

Tôi muốn vận tốc thực tế. RoboJeeves: Được rồi! "Một số lượng, tốc độ thực tế." Me: RJ, những câu chuyện cười giá rẻ dưới phẩm giá của bạn. Bạn có thể hoặc không thể cho tôi biết câu trả lời cho câu hỏi của tôi? RoboJeeves: Có. Tôi: "Có," những gì? RoboJeeves | Chapter 9 Forks in the Road 147 A complete program Listing 9-2 contains a complete program with a simple if statement. The listing s code behaves like an electronic oracle. Ask the program a yes or no question and the program answers you back. Of course the answer to your question is randomly generated. But who cares It s fun to ask anyway. Listing 9-2 I Know Everything import import class AnswerYesOrNo public static void main String args Scanner myScanner new Scanner Random myRandom new Random int randomNumber Type your question my child randomNumber 10 1 if randomNumber 5 Yes. Isn t it obvious else No and don t ask again. Figure 9-4 shows several runs of the program in Listing 9-2. The program s action has four parts Figure 9-4 The allknowing Java program in action. 148 Part III Controlling the Flow__ 1. Prompt the user. Call telling the user to type a question. 2. Get the user s question from the keyboard. In Figure 9-4 I run the AnswerYesOrNo program four times and I type a different question each time. Meanwhile back in Listing 9-2 the statement swallows up my question and does absolutely nothing with it. This is an anomaly but you re smart so you can handle it. Normally when a program gets input from the keyboard the program does something with the input. For instance the program can assign the input to a variable amount Alternatively the program can display the input on the screen But the code in Listing 9-2 is different. When this AnswerYesOrNo program runs the user has to type something. The call to getLine waits for the user to type some stuff and then press Enter. But the AnswerYesOrNo program has no need to store the input for further analysis. The computer does what I .

TỪ KHÓA LIÊN QUAN