private static int readPosInt(String message) { while (true) { System.out.print(message); try { int num = input.nextInt(); if (num > 0) { return num; } System.out.println("Positive integers only!"); } catch (InputMismatchException e) { System.out.println("You must type an integer!"); } } }
时间: 2023-07-16 20:13:05 浏览: 49
这是一个 Java 代码段,它定义了一个名为 `readPosInt` 的静态方法,该方法以一个字符串作为输入参数,并返回一个正整数。
该方法会不断循环读取用户从键盘输入的整数,直到读取到一个正整数为止。如果用户输入的不是整数,则会提示用户重新输入;如果用户输入的是负数或零,则会提示用户输入正整数。
这个方法使用了 Java 标准库中的 `Scanner` 类来读取用户输入。在循环中,如果用户输入的不是整数,`Scanner` 会抛出 `InputMismatchException` 异常,代码会捕获这个异常并提示用户重新输入。如果用户输入的是正整数,则会立即返回该值。
相关问题
帮我完善代码import java.util.InputMismatchException; import java.util.Scanner; public class CLI { private static Scanner input = new Scanner(System.in); private static String readLine(String str1) { System.out.print("Type some text:"); String str = input.nextLine(); return str; } private static int readPosInt(String str2) { while(true) { System.out.print("Type some integer:"); int i; while(true) { try { i= input.nextInt(); System.out.println("i:"+i); break; }catch(InputMismatchException e){ System.out.println("You must type an integer!"); input.nextLine(); } } if(i>=0) { return i; }else { System.out.println("Positive intefers only!"); } } } public static void main(String[] args) { String str1 = readLine("Type some text: "); System.out.println("Text read is: " + str1); int i = readPosInt("Type an integer: "); System.out.println("Integer read is: " + i); String str2 = readLine("Type some text again: "); System.out
.println("Text read is: " + str2); input.close(); } }
你的代码已经很好了,但我稍微修改了一下以更好地处理异常情况并提供更好的用户体验。
import java.util.InputMismatchException;
import java.util.Scanner;
public class CLI {
private static Scanner input = new Scanner(System.in);
private static String readLine(String prompt) {
System.out.print(prompt);
String str = input.nextLine();
return str;
}
private static int readPosInt(String prompt) {
while (true) {
System.out.print(prompt);
try {
int i = input.nextInt();
input.nextLine(); // consume remaining newline character
if (i >= 0) {
return i;
} else {
System.out.println("Positive integers only!");
}
} catch (InputMismatchException e) {
System.out.println("You must type an integer!");
input.nextLine(); // consume invalid input
}
}
}
public static void main(String[] args) {
String str1 = readLine("Type some text: ");
System.out.println("Text read is: " + str1);
int i = readPosInt("Type an integer: ");
System.out.println("Integer read is: " + i);
String str2 = readLine("Type some text again: ");
System.out.println("Text read is: " + str2);
input.close();
}
}
private static void addTeacher() { Scanner scanner = new Scanner(System.in); System.out.print("请输入教师编号:"); int id = scanner.nextInt(); System.out.print("请输入教师姓名:"); String name = scanner.next(); System.out.print("请输入教师性别:"); String gender = scanner.next(); System.out.print("请输入出生日期(格式:yyyy-mm-dd):"); String birthDate = scanner.next(); System.out.print("请输入所在部门:"); String department = scanner.next(); System.out.print("请输入职称:"); String title = scanner.next(); System.out.print("请输入工资:"); double salary = scanner.nextDouble(); teacherList.add(new Teacher(id, name, gender, birthDate, department, title, salary)); System.out.println("教师信息添加成功!"); }增加蛛丝
在这段代码中,我们可以添加一些输入校验逻辑,以保证输入的数据合法性。例如:
1. 检查输入的教师编号是否已经存在于系统中,如果是则提示用户重新输入一个不存在的编号。
2. 对于出生日期的输入,可以使用正则表达式进行格式校验,确保输入的日期符合 yyyy-mm-dd 的格式。
3. 对于工资的输入,可以检查输入的值是否大于等于 0,如果小于 0 则提示用户重新输入。
修改后的代码如下:
```
private static void addTeacher() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入教师编号:");
int id = scanner.nextInt();
// 检查编号是否已存在
while (findTeacherById(id) != null) {
System.out.println("该编号已存在,请重新输入!");
System.out.print("请输入教师编号:");
id = scanner.nextInt();
}
System.out.print("请输入教师姓名:");
String name = scanner.next();
System.out.print("请输入教师性别:");
String gender = scanner.next();
// 校验出生日期格式
String birthDate;
Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
do {
System.out.print("请输入出生日期(格式:yyyy-mm-dd):");
birthDate = scanner.next();
} while (!pattern.matcher(birthDate).matches());
System.out.print("请输入所在部门:");
String department = scanner.next();
System.out.print("请输入职称:");
String title = scanner.next();
// 检查工资是否合法
double salary;
do {
System.out.print("请输入工资:");
salary = scanner.nextDouble();
} while (salary < 0);
teacherList.add(new Teacher(id, name, gender, birthDate, department, title, salary));
System.out.println("教师信息添加成功!");
}
```