The readPosInt method uses System.out.print (not println) to print its string argument on the screen (later when we use the readPosInt method, the string argument of the method will be a message telling the user to type some integer). Then the readPosInt method uses the input scanner object to read an integer from the user of the program. After reading the integer, the readPosInt method must also use the scanner’s nextLine method to read the single newline character that comes from the user pressing the Enter key on the keyboard after typing the integer (if you do not read this newline character using the nextLine method inside the readPosInt method, then the newline character will remain in the input stream, and, the next time you use the readLine method described above, the readLine method will just immediately read only the newline character from the input stream and return an empty string as result, without waiting for the user to type anything!) If the user types something which is not an integer, then the nextInt method of the scanner will throw an InputMismatchException. In that case the code of your readPosInt method must catch the exception, use System.out.println to print the error message "You must type an integer!" to the user (use System.out.println for this, not System.err.println, otherwise you might hit a bug in Eclipse...), use the scanner’s nextLine method to read (and ignore) the wrong input typed by the user of the program (if you do not do this, the wrong input typed by the user will remain in the input stream, and the next time you call the nextInt method again, you will get an InputMismatchException again!), and then do the whole thing again (including printing again the string argument of the readPosInt method) to try to read an integer again (hint: put the whole code of the method inside a while loop). After reading the integer and the newline character (which is just ignored), the readPosInt method tests the integer.写java文件
时间: 2023-12-14 20:39:45 浏览: 90
下面是一个示例的Java代码实现:
```java
import java.util.InputMismatchException;
import java.util.Scanner;
public class ReadPosInt {
public static int readPosInt(String message) {
Scanner sc = new Scanner(System.in);
int num = 0;
boolean validInput = false;
while (!validInput) {
System.out.print(message);
try {
num = sc.nextInt();
sc.nextLine(); // consume the newline character
if (num < 0) {
System.out.println("You must type a positive integer!");
} else {
validInput = true;
}
} catch (InputMismatchException e) {
System.out.println("You must type an integer!");
sc.nextLine(); // consume the wrong input
}
}
return num;
}
public static void main(String[] args) {
int num = readPosInt("Please type a positive integer: ");
System.out.println("You typed: " + num);
}
}
```
在这个例子中,我们使用了一个while循环来重复读取整数,直到用户输入一个正确的正整数为止。在循环内部,我们首先使用System.out.print方法打印提示信息,然后使用Scanner类的nextInt方法读取用户输入的整数。如果读取到的不是整数,会抛出InputMismatchException异常。此时我们使用System.out.println方法输出错误提示信息,并使用nextLine方法读取并忽略掉用户输入的错误内容(以避免将其留在输入流中)。如果读取到的整数小于0,也会输出错误提示信息,并继续重复读取。如果读取到的是一个正确的正整数,则设置validInput为true,跳出循环并返回该整数。最后,在main方法中调用readPosInt方法来读取用户输入的正整数,并输出该整数。
阅读全文