【发布时间】:2017-10-15 23:30:25
【问题描述】:
我正在对我的 Java 程序进行故障排除,以使用 while 循环和 try-catch 语句来引发异常,如果用户输入了 4 个整数输入,而预期只有 3 个。我使用了一个结合了 while 循环和 hasNextInt 方法的数组,但我无法让它运行。任何见解都会很棒。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 3;
int[] numbers = new int[count];
System.out.println("Enter 3 integers");
Scanner numScanner = new Scanner(scanner.nextLine());
while (numbers[] > count) {
if (numScanner.hasNextInt()) {
numbers[] = numScanner.hasNextInt();
} else
{
System.out.println("You didn't provide enough numbers");
break;
}
}
}
【问题讨论】:
-
您的 numbers[] 变量需要方括号内的索引
标签: java arrays while-loop try-catch