Chapter 4 Loops and Files

118 10 0
Chapter 4   Loops and Files

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 4 Chapter 4 Loops and Files 2 Contents 1 The Increment and Decrement Operators 2 The while Loop 3 Using the while Loop for Input Validation 4 The do while Loop 5 The for Loop 6 Running Totals.

Chapter Loops and Files Contents The Increment and Decrement Operators The while Loop Using the while Loop for Input Validation The do-while Loop The for Loop Running Totals and Sentinel Values Nested Loops The break and continue Statements Contents Deciding Which Loop to Use 10 Introduction to File Input and Output 11 The Random Class The Increment and Decrement Operators Java provides a set of simple unary operators designed just for incrementing and decrementing variables  Incrementing: increasing by one  Decrementing: decreasing by one  Increment operator ++  Decrement operator  The Increment and Decrement Operators Incrementing the variable number  number = number + 1; number += 1; number++; ++number; Decrementing a variable  number = number - 1; number -= 1; number ; Postfix and Prefix Modes Postfix mode  The operator is placed after the variable  number++; number ; Prefix mode  The operator is placed before the variable  ++number; number; Postfix and Prefix Modes The Difference between Postfix and Prefix Modes The difference is important if these operators ++ and are used in statements that more than just increment or decrement  Example:  number = 4; System.out.println(number++); This statement does two things: Calls println method to display the value of number  Increments number  But which happens first? The Difference between Postfix and Prefix Modes Postfix mode causes the increment to happen after the value of the variable is used in the expression  number = 4; System.out.println(number++); The println method will display and then number will be incremented to number = 4; System.out.println(number); number = number + 1; The Difference between Postfix and Prefix Modes The prefix mode causes the increment to happen first  number = 4; System.out.println(++number);   number will be incremented to and then The println method will display number = 4;  number = number + 1;  System.out.println(number);  10 Detecting the End of a File Quite often a program must read the contents of a file without knowing the number of items that are stored in the file  The program must detect the end of the file  You can determine when the end of a file has been reached by testing the value returned from the readLine method  If the last item has been read and the end of the file has been reached, the readLine method 104 return null  Detecting the End of a File Open the file Read the first item using the readLine method Did readLine return null ? No Process the item Read the next item using the readLine method Yes Close the file 105 Detecting the End of a File FileReader freader = new FileWriter(filename); BufferedReader inputFile = new BufferedReader(freader); String str; // Read the first item str = inputFile.readLine(); // If an item was read, display it and read the remaining // items while(str != null) { System.out.println(str); str = inputFile.readLine(); } 106 Reading Data from a File Problem: Write a program that reads data from a text file The file contains the names of your friends  107 Reading Data from a File 108 Review You must have the import java.io.*; statement in the top section of your program Because we have not learned how to respond to exceptions, any method that uses a FileReader or BufferedReader object must have a throws IOException clause in its header You create a FileReader object and pass the name of the file as a string to the constructor 109 Review You create a BufferedReader object and pass a reference to the FileReader object as an argument to the constructor You use the BufferedReader object's readLine method to read a linefrom the file The method returns the line of data as a string If the end of the file has been reached, the method returns null When finished reading from the file, you use the BufferedReader object's close method to 110 close the file Converting String Values to Numbers Problem: Write a program that reads a text file Numbers.txt The file contains a series of numbers The program calculates the total of the numbers  111 Converting String Values to Numbers 112 Checkpoint 4.16  4.17  4.18  4.19  4.20  4.21  4.22  4.23  113 11 The Random Class The Random class is used to generate a sequence of random numbers  Using Random class  import java.util.Random;  Random randomNumbers = new Random();  Use the Random class's method to generate random numbers  114 11 The Random Class Some of the Random class's methods  nextDouble(): Returns the next random number as a double The number will be within the range of 0.0 and 1.0  nextFloat(): Returns the next random number as a float The number will be within the range of 0.0 and 1.0  nextInt(): Returns the next random number as an int  nextInt(int n): Returns the next random number as an int The number will be within the range of and n  nextLong(): Returns the next random number as a long  115 11 The Random Class Problem: Write a Java program displays an addition problem, and allows the user to enter the answer Then the program checks whether the answer is correct  116 117 118 ... Increment and Decrement Operators The while Loop Using the while Loop for Input Validation The do-while Loop The for Loop Running Totals and Sentinel Values Nested Loops The break and continue... Introduction to File Input and Output 11 The Random Class The Increment and Decrement Operators Java provides a set of simple unary operators designed just for incrementing and decrementing variables... variable  ++number; number; Postfix and Prefix Modes The Difference between Postfix and Prefix Modes The difference is important if these operators ++ and are used in statements that more

Ngày đăng: 27/09/2022, 22:17

Tài liệu cùng người dùng

Tài liệu liên quan