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

Đây là chuỗi các sự kiện từ đầu để kết thúc thí nghiệm: 1. Trước khi tôi chạy mã trong Ví dụ 13-5, ổ đĩa cứng máy tính của tôi đã không có tập tin . Đó là okay. mỗi thí nghiệm Một lớp học có thể xác định cách bạn sử dụng dữ liệu. | 272 Part IV Using Program Units_ Figure 16-1 Occupancy data for the Java Motel. You want a report showing the number of guests in each room. Because you know how many rooms you have this problem begs for a for loop. The code to solve this problem is in Listing 16-1 and a run of the code is shown in Figure 16-2. Listing 16-1 A Program to Generate an Occupancy Report import import import import static class ShowOccupancy public static void main String args throws FileNotFoundException Scanner diskScanner new Scanner new File occupancy Room tGuests for int roomNum 0 roomNum 10 roomNum roomNum t General Output Figure 16-2 Running the code in Listing 16-1. Roon Guests 0 1 1 4 2 0 3 2 4 2 s 1 6 4 7 3 3 0 9 2 Chapter 16 Using Loops and Arrays 273 Listing 16-1 uses a for loop a loop of the kind described in Chapter 15. As the roomNum variable s value marches from 0 to 9 the program displays one number after another from the occupancy file. To read more about getting numbers from a disk file like my occupancy file see Chapter 13. This example s input file is named occupancy not . If you use JCreator s File Wizard to make an occupancy file you must put a dot in the wizard s Name field. That is you must type occupancy. occupancy- dot in the Name field. If you don t type your own dot anywhere in the Name field then JCreator adds a default extension to the file s name turning occupancy into . Grabbing input here and there Listing 16-2 illustrates some pithy issues surrounding the input of data. For one thing the program gets input from both the keyboard and a disk file. The program gets a room number from the keyboard. Then the program gets the number of guests in that room from the occupancy file. To make this happen Listing 16-2 sports two Scanner declarations

TỪ KHÓA LIÊN QUAN