简体   繁体   English

如何解决 java.lang.NumberFormatException:对于输入字符串:“TooLow”

[英]How can I solve java.lang.NumberFormatException: For input string: “TooLow”

I am trying to create a set of code that allows you to play guess the number with the computer.我正在尝试创建一组代码,让您可以用计算机猜数字。 The problem section and the debugging console section are both clear and I was able to run it until I tried to input values through the intergrated terminal.问题部分和调试控制台部分都很清楚,我能够运行它,直到我尝试通过集成终端输入值。

I have converted the returned string value from Input.nextLine() to an int value although that seemed to have no effect, I also switched from running the code on the intergrated terminal instead of the debugging console.我已将返回的字符串值从 Input.nextLine() 转换为 int 值,尽管这似乎没有效果,但我也从在集成终端而不是调试控制台上运行代码切换。 In addition, I tried the catch(NumberFormatException) statement but it failed as Visual Studio Code can't identify catch nor NumberFormatException.此外,我尝试了 catch(NumberFormatException) 语句,但它失败了,因为 Visual Studio Code 无法识别 catch 或 NumberFormatException。

while ( win == false)
     {
int CorrectNumber = 70;
int g = (int)( Math.random() * 101 );
System.out.print( "The Number I Guess Is:" + g);

String TooHigh = "TooHigh";
//type TooHigh if the number computer guessed is too high 

TooHigh = input.nextLine();
int h1 = Integer.parseInt(TooHigh);

String TooLow = "TooLow";
//type TooLow if the number computer guessed is too low

TooLow = input.nextLine();
int l1 = Integer.parseInt(TooLow);

if ( g == h1 ) //too high
{
    int y = (int)( Math.random() * ( g - 70 ) );
    System.out.print( "Another Number I Guessed is: " + y );
}
else if ( g == l1 ) //too low
{
    int z = (int)( Math.random() * ( 70 - g ) );
    System.out.print( "Another Number I Guessed is: " + z );
}
else if ( g == 70 ) //computer wins
{
    win = true;
}

When I input words to tell the computer whether or not the number it guessed is too high or too low, I expect it giving me another number.当我输入文字告诉计算机它猜到的数字是太高还是太低时,我希望它会给我另一个数字。 But instead, I get the following messages:但相反,我收到以下消息:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "TooLow" 
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
            at java.base/java.lang.Integer.parseInt(Integer.java:652)
            at java.base/java.lang.Integer.parseInt(Integer.java:770)
            at RevGuess.main(RevGuess.java:23)

Why are you trying to get an int from the String that is for telling that the guess was too low?你为什么要从 String 中获取一个 int 来说明猜测太低了? That can just be treated as a String and then you can compare Strings with.equals to see if the Strings' values are the same.可以将其视为字符串,然后您可以将字符串与.equals 进行比较,以查看字符串的值是否相同。 If they are, you tell the program that it is too low or to high.如果是,你告诉程序它太低或太高。

You are comparing int with a String that is why you are getting the error.您正在将 int 与 String 进行比较,这就是您收到错误的原因。 What you should do is compare the user input with toohigh or toolow and execute the too low or too high portion of your if/else depending on the user input您应该做的是将用户输入与太高或太低进行比较,并根据用户输入执行 if/else 的太低或太高部分

Base on your source code, java can not convert word "TooLow" to integer.根据您的源代码,java 无法将单词“TooLow”转换为 integer。 it is still bias about your goal, if you want to compare integer always input number, so the code int h1 = Integer.parseInt(TooHigh);如果你想比较 integer 总是输入数字,它仍然对你的目标有偏见,所以代码 int h1 = Integer.parseInt(TooHigh); will work perfectly.将完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 java.lang.NumberFormatException:对于输入字符串:“396.00” - How to solve java.lang.NumberFormatException: For input string: "396.00" 我该如何解决:java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NumberFormatException:对于输入字符串:“ s%”? - How do I solve: java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NumberFormatException: For input string: “s%”? 如何防止 java.lang.NumberFormatException:对于输入字符串:“N/A”? - How can I prevent java.lang.NumberFormatException: For input string: "N/A"? 如何解决 java.lang.NumberFormatException: empty String - How to solve java.lang.NumberFormatException: empty String 我不知道如何解决代码,因为我不断收到 java.lang.NumberFormatException 的反馈:对于输入字符串:“” - I don't know how to solve the code as i keep having the feedback of java.lang.NumberFormatException: For input string: "" 如何解决这个 java.lang.NumberFormatException: For input string: "" 基于我的代码? - How to solve this java.lang.NumberFormatException: For input string: "" based on my code? 如何在 Java 中调试“java.lang.NumberFormatException: For input string: X”? - How to debug "java.lang.NumberFormatException: For input string: X" in Java? 如何在回文中解决此java.lang.NumberFormatException? - How to solve this java.lang.NumberFormatException in palindrome? 我收到错误java.lang.NumberFormatException:对于输入字符串:Excel - I got error java.lang.NumberFormatException: For input string:Excel 我不断收到 java.lang.NumberFormatException:For input string:"" - I keep getting java.lang.NumberFormatException:For input string:""
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM