输入不匹配错误 [英] Input mismatch error

查看:105
本文介绍了输入不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在这里有这个小程序,并且遇到了输入不匹配错误.我将在代码下方发布错误消息.老实说,我有0条线索为什么会出现错误.我检查了所有必需的变量是否分配了正确的类型.如果我理解正确,那么当您尝试输入不需要的内容时,就会发生上述错误,对吗? (而不是int或其他类型).由于某些原因,我可能在某个我看不到的地方犯了一些愚蠢的错误,因此,感谢您的帮助.

So, I have this little program here and I've been getting the input mismatch error. I'll post the error message below the code. To be honest, I have 0 clue why the error appears. I've checked that all the required variables have correct type assigned. If I understand correctly, the said error happens when you try to input something that is not expected, right? ( double instead of int or whatever). I probably did some stupid mistake somewhere that I cant see for some reason, so I'd appreciate your help.

    Scanner sc = new Scanner(System.in);

    int enEur = 0;
    int dvaEur = 0;
    boolean bankrot = false;
    int placilo;


    while (sc.hasNextInt() || bankrot == false){
        placilo = sc.nextInt();
         if(placilo == 1){
             enEur += placilo;
         }
         else{
             enEur --;
             bankrot = test(enEur);
             dvaEur ++;
         }
     }

所以,这就是我的main()方法的内容.该错误显示在以下行中:placilo = sc.nextInt().这是上述错误的副本:

So, this is the content of my main() method. The error appears in the line: placilo = sc.nextInt(). Here's the copy of the said error:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at evroSop.main(evroSop.java:16)

我输入的内容基本上是一行单独的整数,例如:1 1 1 1 2 1 ...等.我有不同的示例,所有示例都是不同长度的整数行.

The input that I put in is basically a line of separate integers like: 1 1 1 1 2 1... and so on. I've got different examples, all of which are lines of integers of different lengths.

乔治·切里奥

推荐答案

条件while (sc.hasNextInt() || bankrot == false)意味着只要有更多输入 bankrot仍为假.因此,如果您用尽了所有输入并且仍然没有破产,那么您将得到一个异常,因为即使没有更多输入,您也将尝试读取下一个输入.

The condition while (sc.hasNextInt() || bankrot == false) means that you'll continue trying to read from the scanner as long as there is more input or bankrot is still false. So, if you run out of inputs and still aren't bankrupt, you'll get an exception because you'll try reading next input even though there is no more of it.

条件应该是,而不是.

这篇关于输入不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆