Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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 在我的上下文中同时使用nextLine()和nextInt()总是会出错_Java_Debugging_Java.util.scanner - Fatal编程技术网

Java 在我的上下文中同时使用nextLine()和nextInt()总是会出错

Java 在我的上下文中同时使用nextLine()和nextInt()总是会出错,java,debugging,java.util.scanner,Java,Debugging,Java.util.scanner,我正在编写一些Java代码来制作一个猜谜游戏,根据你的最大值生成一个随机数,你必须猜出正确的数字。您还可以设置可以尝试的次数。这就是问题发生的地方。您可以以数字形式设置尝试次数或写出“无限”。我这里有一个这样做的代码示例,其中有一些注释可以帮助您: import java.util.Scanner; public class Game{ public static int processMaxAttempts; public static Scanner maxAttempts; publi

我正在编写一些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
发生的是一个get错误,该错误说明:

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天),我有点无助。我很想知道我的问题是什么,这样我就可以在将来应用它并编写一些很酷的游戏

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

您需要的是:

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

我想这就是你要找的

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

}
公共类测试
{
私有整数可猜测数;
私有整数;
公开考试()
{
最大尝试次数=0;
}
公共物品{
扫描仪扫描=新扫描仪(System.in);
随机=新随机();
System.out.println(“请输入您的猜测数量或键入无限猜测”);
字符串s=scan.next();
如果(s.toUpperCase().equals(“无限”)){
猜测数=随机数。下一个(100);
}
否则{
试一试{
maxAttempts=Integer.parseInt(s);
猜数=random.nextInt(100)+Integer.parseInt(s);
}捕获(例外e){
System.out.println(“您没有输入有效的最大尝试次数”);
}
}
int计数器=0;
System.out.println(“键入猜测”);
while(scan.nextInt()!=guessableNumber&计数器最大尝试次数){
System.out.println(“您已超过最大尝试次数”);
}
否则{
System.out.println(“正确您猜到了正确的数字:”+GustableNumber);
}
}
公共静态void main(字符串[]args)
{
测试=新测试();
test.doYourStuff();
}
}

一个对我一直有效的小技巧就是继续做第二个扫描器,即num和text,这样你就可以让一个扫描器查找int值,另一个扫描器处理字符串。

你第一次输入的是什么?请在
系统中发布内容。在
(你正在键入的内容)对不起,我不太明白你的意思。在我的编译器中,它给了我一个空行,我可以输入。文本输入。然后我点击回车键,它继续代码。正如我所说,我对Java非常陌生:(如果在中键入“unlimited”,那么调用
maxtures.nextInt(),你希望发生什么?你告诉它读<代码> INT/CUT>,但是它找到了<代码>字符串< /代码>,所以你的输入不符合预期。你可能想考虑改变你的代码,例如,输入<代码> 0 表示无限的尝试,而不是混合<代码>字符串< /代码>和<代码> int。/代码>一起输入。那么什么时候输入所需输入的数字?是否直接按enter键?好的。谢谢。只需几个问题:一:“hasNextInt()”是什么意思?二:&&thing的意思是“也”还是别的?我是Java的新手,这就是为什么我会问这些愚蠢的问题。没问题@AnnualMelons,这将检查是否有一个整数可以从扫描仪读取