我试图捕捉最终起作用的异常,但现在我需要弄清楚如何让它循环。另外,我怎样才能让用户输入(int select)到循环之外?我可以尝试创建一个新功能,这可能会奏效。
do {
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
select = intFind.nextInt();
} catch (InputMismatchException e) {
// Display the following text in the event of an invalid input
System.out.println("Invalid input!");
intFind.nextLine();
}
} while (select < 0);
然后
do
{
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
b = Get();
}
catch (InputMismatchException e) {
// Display the following text in the event of an invalid input
System.out.println("Invalid input!");
intFind.nextLine();
} copySel = b;
}while(select < 0);
和
static int Get()
{
Scanner intFind = new Scanner(System.in);
int select;
select = intFind.nextInt();
return select;
}