当我运行以下代码时,它会清除控制台但似乎没有做任何事情。
int x = 0; //exception catching loop
while (x == 0);
ArrayList<Integer> values = new ArrayList<Integer>();
Scanner Scores = new Scanner(System.in);//creates a scanner for the golfer's last 5 scores
System.out.println("Please enter the scores from your last five rounds of eighteen-hole golf, in order.");
try
{
for(int i = 0; i < 5; i++)
{
System.out.println("Please enter one score.");
int scorecard = Scores.nextInt();
values.add(scorecard);
}
}catch(InputMismatchException ex){
System.out.println("There has been an error. Please enter your last five scores again.");
x = 1; //if successful, will allow for continuation of the program.
}
但是,当我取出 while 循环和 try-catch 行时,程序运行良好,我只是不确定如何正确执行此段。它没有显示错误,但无法运行。困扰我的部分是我似乎无法正确编码异常捕获系统。(我是 Java 新手。)