-1

我最近问了一个关于这段代码的问题,但我还有另一个问题。我想将扫描仪设置为只接受整数,因为当我在测试中输入一个字母时,我得到这个错误:

 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!");
}

}

4

4 回答 4

1

你有几个选择:

  • 用于Scanner.hasNextInt()查看是否可以读取整数;请注意,next()如果令牌不是整数,则必须跳过令牌,以免“卡住”。
  • 将标记读取为字符串并使用Integer.parseInt(),忽略无法解析为整数的标记。
于 2013-11-05T22:01:57.543 回答
1

我认为一个解决方案是使用一个while块,并测试直到输入是一个整数,就像这样:

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");
     }

}

我没有对此进行测试,但我认为它应该可以工作;)

于 2013-11-05T22:02:13.823 回答
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;
     }
于 2014-08-13T11:34:42.340 回答
0

这里有几个选项

  1. 您可以将 input.nextInt() 包含在 try/catch 块中并捕获 InputMismatchException。然后,您可以再次提示他们或随意处理。

    try {
       P1 = input.nextInt();
    } catch (InputMismatchException e) {
       //Handle how you choose.
    }
    
  2. Scanner有一个 hasNextInt() 方法,如果下一个标记是 int,它将评估为 true。

    if(input.hasNextInt()){
       P1 = input.nextInt();
    } else {
       //Handle how you choose. 
    } 
    
于 2013-11-05T22:11:00.633 回答