我需要验证用户是否输入了两个整数,因此,我需要继续要求他输入,直到他提供两个整数输入.不知道如何实现它,但我想出了类似的方法,但现在很难实现判断coord1和coord2是否得到正确类型的部分.如果不是,它当然会给我数字形式的感觉:
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语法和验证方法.