假设我有这个代码:
public static void main(String[] args)
{
Scanner key01 = new Scanner(System.in);
int input;
System.out.println("Please enter a int.");
while (key01.hasNext())
{
if(key01.hasNextInt())
{
input = key01.nextInt();
System.out.println("Thanks for input!");
}
else
{
System.out.println("You must enter an Int");
key01.next(); //Why does this statement prevent an infinite loop?
//That statement prevents
//the program from constantly printing "you must enter an int"
}
}
}
key01.next() 如何防止控制台不断打印出错误消息?
叮当猫咪
一只名叫tom的猫
相关分类