Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅允许使用java scanner输入整数_Java - Fatal编程技术网

仅允许使用java scanner输入整数

仅允许使用java scanner输入整数,java,Java,我最近问了一个关于这个代码的问题,但我有另一个问题。我想将扫描仪设置为只接受整数,因为当我在测试中输入字母时,会出现以下错误: 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.Scanne

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

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

}您有两个选择:

使用Scanner.hasNextInt查看是否可以读取整数;请注意,如果标记不是整数,则必须使用next跳过该标记,以免卡住。 将令牌作为字符串读取,并使用Integer.parseInt,忽略无法作为整数解析的令牌。
我认为解决方案是使用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");
     }

}

我还没有测试过这个,但我认为它应该可以工作

这里有几个选项

您可以将input.nextInt包围在try/catch块中,并捕获InputMismatchException。然后,您可以再次提示他们,或者根据自己的喜好处理此问题

try {
   P1 = input.nextInt();
} catch (InputMismatchException e) {
   //Handle how you choose.
}
具有一个hasNextInt方法,如果下一个标记是int,则该方法的计算结果将为true

if(input.hasNextInt()){
   P1 = input.nextInt();
} else {
   //Handle how you choose. 
} 

您可以大大简化代码。如果P1-P2==0{it's a tie}。您可以将一个字符作为输入并手动检查该值,或者使用try/catch块捕获错误。请参阅@MarcB的构建,您可以使用一些模块化算法来获得赢家,int winner=P1-P2+3%3。如果获胜者是1,则玩家1获胜;若获胜者为2,则玩家2获胜;否则,如果获胜者为0,则为平局。您能否详细说明第二个选项?这基本上就是我想要的,没有其他的。我相信如果返回的语句中没有整数1、2或3,程序就会循环。hasNextInt会检查下一个标记是否为int。例如,如果用户输入“Hello”,那么这将失败,因为它显然不是int。但是,如果他们输入诸如“5”之类的值,这将通过if块并获取整数值。如果他们没有传递一个整数,您仍然需要通过添加类似于上面的input.next的内容来移动扫描仪。您需要始终记住获取用户提供给您的输入,即使它不是int。此外,我在上面的Scanner上添加了一个链接,其中将包含有关hasnetint方法的更多信息。您能为我的案例解释Scanner.hasnetint方法吗?我已经试了半个小时了,但是我不能让它工作。你可以使用这个方法…这个方法会重复要求整数输入,直到它得到整数输入。。。
     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;
     }