【发布时间】:2013-05-27 12:45:36
【问题描述】:
在下面的代码中,我要求用户输入一个整数,如果输入是 0 或负数,它会再次循环,直到给出正数。问题是,如果用户按下一个字母,我的代码就会崩溃,尽管我在很多方面都使用了 try-catch,但并没有真正奏效。有任何想法吗? 我在循环中使用了 try-catch,但它只适用于一个字母输入,并且不正确。
System.out.print("Enter the number of people: ");
numberOfPeople = input.nextInt();
while (numberOfPeople <= 0) {
System.out.print("Wrong input! Enter the number of people again: ");
numberOfPeople = input.nextInt();
}
【问题讨论】:
标签: java while-loop try-catch java.util.scanner