在收到 2 个整数之前如何要求输入?
How to ask for input until 2 integers are received?
我需要验证用户输入了两个整数,因此,我需要继续询问他的输入,直到他提供两个都是整数的输入。不确定如何实现它,但我想出了类似的东西,但现在正在努力实现检查 coord1 和 coord2 是否获得正确类型的部分。如果没有,它当然会给我 NumberFormatException:
while (true) {
System.out.print("Enter the coordinates: ");
int coord1 = Integer.parseInt(scanner.next());
int coord2 = Integer.parseInt(scanner.next());
if (coord1 < 1 || coord1 > 3 || coord2 < 1 || coord2 > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
} else if (cellOccupied(field, coord1, coord2)) {
System.out.println("This cell is occupied! Choose another one!");
continue;
}
break;
}
不使用 try / catch 可以解决吗,因为我还没有学会,或者这是唯一的方法吗?
在此先感谢您,抱歉,因为我仍在学习 Java 语法和验证方式。
您可以找到 here 几条建议。例如,您可以使用正则表达式。创建一个 isNumeric
函数来告诉您给定的字符串是否为整数:
public boolean isNumeric(String strNum) {
Pattern pattern = Pattern.compile("\d+");
if (strNum == null) {
return false;
}
return pattern.matcher(strNum).matches();
}
在将 scanner.next()
推送到整数解析器之前,请使用函数对其进行检查。
您可以依赖 Scanner
的方法 hasNextInt()
和 nextInt()
.
,而不是手动检查输入的类型是否正确
第一个将检查您的输入是否是一个实际的 int,然后您可以使用 nextInt()
继续阅读它。有关在读取数字类型后放置 nextLine()
的更多详细信息,请阅读以下关于堆栈溢出的 question。
在这里,我还将您的代码包含在示例 main 中。我知道你的只是一个包含更多代码的片段(例如,我没有 cellOccupied
方法)但我只是像这样粘贴它以进行最小测试。此外,我还参数化了您的用例。重复相同的代码来读取应用相同 coordinate-logic.
的用户输入有点奇怪和多余
public class Main {
public static void main(String[] args) {
int coord1 = 0, coord2 = 0;
do {
coord1 = readCoordinate("Enter first coordinate: ");
coord2 = readCoordinate("Enter second coordinate: ");
//Showing an error message if the coords refer to an occupied cell
if (cellOccupied(field, coord1, coord2)) {
System.out.println("This cell is occupied! Choose another one!");
}
} while (cellOccupied(field, coord1, coord2));
}
private static int readCoordinate(String message) {
int coord;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(message);
if (scanner.hasNextInt()) {
coord = scanner.nextInt();
//getting rid of the new line character after reading the int
scanner.nextLine();
//Checking coordinate value
if (coord < 1 || coord > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
} else {
//assigning an undesired value (since your coords must be between 1 and 3
coord = 0;
//getting rid of the wrong user input
scanner.nextLine();
//Showing an error message
System.out.println("Please enter an int value");
//Skipping directly to the loop's condition
continue;
}
break;
}
return coord;
}
}
附带说明,避免在循环中声明字段。
我需要验证用户输入了两个整数,因此,我需要继续询问他的输入,直到他提供两个都是整数的输入。不确定如何实现它,但我想出了类似的东西,但现在正在努力实现检查 coord1 和 coord2 是否获得正确类型的部分。如果没有,它当然会给我 NumberFormatException:
while (true) {
System.out.print("Enter the coordinates: ");
int coord1 = Integer.parseInt(scanner.next());
int coord2 = Integer.parseInt(scanner.next());
if (coord1 < 1 || coord1 > 3 || coord2 < 1 || coord2 > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
} else if (cellOccupied(field, coord1, coord2)) {
System.out.println("This cell is occupied! Choose another one!");
continue;
}
break;
}
不使用 try / catch 可以解决吗,因为我还没有学会,或者这是唯一的方法吗?
在此先感谢您,抱歉,因为我仍在学习 Java 语法和验证方式。
您可以找到 here 几条建议。例如,您可以使用正则表达式。创建一个 isNumeric
函数来告诉您给定的字符串是否为整数:
public boolean isNumeric(String strNum) {
Pattern pattern = Pattern.compile("\d+");
if (strNum == null) {
return false;
}
return pattern.matcher(strNum).matches();
}
在将 scanner.next()
推送到整数解析器之前,请使用函数对其进行检查。
您可以依赖 Scanner
的方法 hasNextInt()
和 nextInt()
.
第一个将检查您的输入是否是一个实际的 int,然后您可以使用 nextInt()
继续阅读它。有关在读取数字类型后放置 nextLine()
的更多详细信息,请阅读以下关于堆栈溢出的 question。
在这里,我还将您的代码包含在示例 main 中。我知道你的只是一个包含更多代码的片段(例如,我没有 cellOccupied
方法)但我只是像这样粘贴它以进行最小测试。此外,我还参数化了您的用例。重复相同的代码来读取应用相同 coordinate-logic.
public class Main {
public static void main(String[] args) {
int coord1 = 0, coord2 = 0;
do {
coord1 = readCoordinate("Enter first coordinate: ");
coord2 = readCoordinate("Enter second coordinate: ");
//Showing an error message if the coords refer to an occupied cell
if (cellOccupied(field, coord1, coord2)) {
System.out.println("This cell is occupied! Choose another one!");
}
} while (cellOccupied(field, coord1, coord2));
}
private static int readCoordinate(String message) {
int coord;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(message);
if (scanner.hasNextInt()) {
coord = scanner.nextInt();
//getting rid of the new line character after reading the int
scanner.nextLine();
//Checking coordinate value
if (coord < 1 || coord > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
} else {
//assigning an undesired value (since your coords must be between 1 and 3
coord = 0;
//getting rid of the wrong user input
scanner.nextLine();
//Showing an error message
System.out.println("Please enter an int value");
//Skipping directly to the loop's condition
continue;
}
break;
}
return coord;
}
}
附带说明,避免在循环中声明字段。