我被困在程序的这部分用户输入多个整数,只有第一个被选中。在我当前的代码中,输出是这样的。忽略/清除用户输入的额外整数
There are 10 sticks on the board.
Jerry: How many sticks do you take (1-3)? *1 3 4 2*
There are 9 sticks on the board.
Sherry: How many sticks do you take (1-3)? There are 6 sticks on the board.
Jerry: How many sticks do you take (1-3)? Please enter a number between 1 and 3.
Jerry: How many sticks do you take (1-3)? There are 4 sticks on the board.
Sherry: How many sticks do you take (1-3)?
虽然在给出的例子中,输出是这样的。
Jerry: How many sticks do you take (1-3)? *1 3 4 2*
There are 8 sticks on the board.
Pat: How many sticks do you take (1-3)? *3*
There are 5 sticks on the board.
似乎额外的整数正在排队等待下一个扫描仪输入命令。在给定的例子中,额外的整数被清除。这是如何完成的?是否有清除所有当前用户输入的命令,或是使用.hasNextInt读入的所有整数,并且第一个过去的数据只是简单地丢弃?另外,hasNextInt在输入中查找的地方是什么?如果没有添加新输入,那么在得到真或假之后,下一个hasNextInt命令是否会查看相同的输入位置?
(由**包围的文字是用户输入。)
请在问题中包含您的代码。 – fvu
添加您的代码。我猜你使用'while(sc.hasNextInt()){}'而不是'if'。 –