在我的上下文中一起使用nextLine()和nextInt()总是会出错

时间:2014-07-16 11:26:53

标签: java debugging java.util.scanner

我正在编写一些可以制作猜谜游戏的Java代码,根据您的最大值生成随机数,您必须猜出正确的数字。您还可以设置可以获得的尝试次数。这就是问题发生的地方。

您可以看到,您可以设置数字形式的尝试次数或写出“无限制”。我有一个代码示例,在这里用注释来帮助你:

import java.util.Scanner;

public class Game{

public static int processMaxAttempts;
public static Scanner maxAttempts;
public static String processMaxAttempts2;

public static void main(String args[]){
//Prints out text
System.out.println("Fill in your maximum attempts OR write \"unlimited\".");
//Creates a scanner
maxAttempts = new Scanner(System.in);
//Looks at the scanner "maxAttempts" and reads its integer value
processMaxAttempts = maxAttempts.nextInt();
//Looks at the scanner "maxAttempts" and reads its string value
processMaxAttempts2 = maxAttempts.nextLine();
//Prints out "unlimited" if "maxAttempts" has a string value and "set" if it has an integer value
if(processMaxAttempts2.equals("unlimited")){
System.out.println("unlimited");
}else{
System.out.println("set");
}//Close else
}//Close main method
}//Close class

如果出现错误,说明了这一点:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at com.pixelparkour.windows.MainGameWindow.main(MainGameWindow.java:34)

该错误针对这行代码:

processMaxAttempts = maxAttempts.nextInt();

所以......是的我不知道。我是Java的新手(我已经学习了3天)而且我有点无助。我很想知道我的问题是什么,所以我可以将它应用到未来并编写一些很酷的游戏!

3 个答案:

答案 0 :(得分:1)

在阅读内容之前,您需要检查内容类型。

您需要的是:

if(maxAttempts.hasNextInt()){ // this will check if there is an integer to read from scanner 
       processMaxAttempts = maxAttempts.nextInt();
} else {
       processMaxAttempts2 = maxAttempts.nextLine();
}

if(processMaxAttempts2!=null && processMaxAttempts2.equals("unlimited")){
    System.out.println("unlimited");
}else{
    System.out.println("set");
}

答案 1 :(得分:0)

我认为这就是你要找的东西

public class Test
{
private int guessableNumber;
private Integer maxAttempts;

public Test()
{
    maxAttempts = 0;
}

public void doYourStuff(){
    Scanner scan = new Scanner(System.in);
    Random random = new Random();
    System.out.println("Please enter your amount of guesses or type unlimited for unlimited guesses");
    String s = scan.next();
    if(s.toUpperCase().equals("UNLIMITED")){
        guessableNumber = random.nextInt(100);
    }
    else {
        try{
            maxAttempts = Integer.parseInt(s);
            guessableNumber = random.nextInt(100) + Integer.parseInt(s);
        }catch(Exception e){
            System.out.println("You did not enter a valid number for max attempts");
        }

    }
    int counter = 0;
    System.out.println("Type in a guess");
    while(scan.nextInt() != guessableNumber && counter <=maxAttempts){
        System.out.println("You did not guess correctly try again");
        ++counter;
    }
    if(counter > maxAttempts){
        System.out.println("You have exceeded your max attempts");
    }
    else {
        System.out.println("Correct you guessed the correct number: "+ guessableNumber);
    }
}

public static void main(String[] args)
{
    Test test = new Test();
    test.doYourStuff();
}

}

答案 2 :(得分:0)

总是对我有用的一个小技巧就是继续制作第二个扫描仪,即num和text,这样你就可以总是找到一个寻找int值而另一个处理字符串。