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

xuống ba giây trong chân không. RoboJeeves: Tất cả các quyền, tôi sẽ. "Vận tốc của một đối tượng sau khi đã giảm trong ba giây trong chân không." Có, tôi nói với nó cho bạn. Me: RoboJeeves, không cung cấp cho tôi rằng câu trả lời thông minh alecky. Tôi muốn có một số. | 106 Part II Writing Your Own Java Programs Figure 7-2 Next thing you know I ll have seventy kids and a thousand gumballs. General Output ---------------------Configuration Chapt How nany gumballs How many kids 80 6 Each kid gets 13 gunballs You should notice a couple of things about Listing 7-2. First you can read an int value with the nextInt method. Second you can issue successive calls to Scanner methods. In Listing 7-2 I call nextInt twice. All I have to do is separate the numbers I type by blank spaces. In Figure 7-2 I put one blank space between my 80 and my 6 but more blank spaces would work as well. This blank space rule applies to many of the Scanner methods. For example here s some code that reads three numeric values gumballs CostOfGumballs kids Figure 7-3 shows valid input for these three method calls. Figure 7-3 Three numbers for three Scanner method calls. General Output --------Conf iguratio 6 What you read is what you get When you re writing your own code you should never take anything for granted. Suppose you accidentally reverse the order of the gumballs and kids assignment statements in Listing 7-2 Chapter 7 Numbers and Types 107 This code is misleading many gumballs How many kids kids gumballs Then the line How many gumballs How many kids is very misleading. Because the kids assignment statement comes before the gumballs assignment statement the first number you type becomes the value of kids and the second number you type becomes the value of gumballs. It doesn t matter that your program displays the message How many gumballs How many kids . What matters is the order of the assignment statements in the program. If the kids assignment statement accidentally comes first you can get a strange answer like the zero answer in Figure 7-4. That s how int division works. It just cuts off any remainder. .

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
5    80    1    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.