我最近询问了有关此代码的问题,但我还有另一个问题。我想将扫描程序设置为仅接受整数,因为当我在测试中输入一个字母时,我收到此错误:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at RockPaperScissorsTest.main(RockPaperScissorsTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
那么,我该怎么做只允许扫描仪中的整数?因此,如果您键入一个,它将不会在扫描仪框中显示任何内容。谢谢您的帮助!以下是整个计划。
import java.util.Scanner;
public class RockPaperScissorsTest {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System. in );
int P1;
int P2;
do {
System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
P1 = input.nextInt();
} while (P1 != 1 && P1 != 2 && P1 != 3);
System.out.println("");
System.out.println("");
System.out.println("");
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
P2 = input.nextInt();
} while (P2 != 1 && P2 != 2 && P2 != 3);
if (P1 == 1 & P2 == 1)
System.out.println("It's a tie!");
if (P1 == 1 & P2 == 2)
System.out.println("Player 2 wins!");
if (P1 == 1 & P2 == 3)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 1)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 2)
System.out.println("It's a tie!");
if (P1 == 2 & P2 == 3)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 1)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 2)
System.out.println("Player 1 wins");
if (P1 == 3 & P2 == 3)
System.out.println("It's a tie!");
}
}
答案 0 :(得分:1)
您有几个选择:
Scanner.hasNextInt()
查看是否可以读取整数;请注意,如果令牌不是整数,则必须使用next()
跳过令牌,这样就不会“卡住”。Integer.parseInt()
,忽略无法解析为整数的标记。答案 1 :(得分:1)
我认为解决方案是使用while块,并测试直到输入为Integer,如下所示:
int choice;
Scanner sc = new Scanner(System.in);
String input = "a";
boolean notAnInteger = true;
while(notAnInteger){
input = sc.next();
try{
choice = Integer.parseInt(input);
notAnInteger = false;
}catch(Exception e){
System.out.println("Not an integer");
}
}
我没有对此进行测试,但我认为它应该可行;)
答案 2 :(得分:1)
boolean testing = false;
String pos = "";
while(true)
{
testing = false;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the integer value.... ");
pos = sc.next();
for(int i=0; i<pos.length();i++)
{
if(!Character.isDigit(pos.charAt(i)))
testing = true;
}
if(testing == true)
{
System.out.print("Enter only numbers.. ");
continue;
}
else
{
int key = Integer.parseInt(pos);
// Your code here....
break;
}
答案 3 :(得分:0)
这里有几个选项
您可以在try / catch块中包围input.nextInt()并捕获InputMismatchException。然后你可以再次提示它们或者你喜欢处理它。
try {
P1 = input.nextInt();
} catch (InputMismatchException e) {
//Handle how you choose.
}
Scanner有一个hasNextInt()方法,如果下一个标记是int,它将评估为true。
if(input.hasNextInt()){
P1 = input.nextInt();
} else {
//Handle how you choose.
}