简体   繁体   English

做..虽然循环不断重复,但扫描仪问题

[英]Do..While loop keeps repeating, Scanner issue

do {   
    System.out.println("Set the A param: ");
    if(input.hasNextDouble() == true) {
        A = input.nextDouble();
        if(A == 0) {
            System.out.println("Param A cannot be a 0!");
        }
    } else if(input.hasNextDouble() == false) {
        System.out.println("Param A must be a number!");
    }
} while(A == 0 || input.hasNextDouble() == false);

Hello, I'm really new in Java and I found an obstacle I can't resolve by myself.你好,我是 Java 新手,我发现了一个我自己无法解决的障碍。

Everything is okay until I enter some letter instead of number, then this do..while loop keeps repeating itself.一切正常,直到我输入一些字母而不是数字,然后这个 do..while 循环不断重复。

After some search I suppose this might be a problem with a Scanner buffer becouse I should clear it before every loop with input.nextLine() but I don't really know where in code should I put it.经过一番搜索,我想这可能是 Scanner 缓冲区的问题,因为我应该在每次使用input.nextLine()循环之前清除它,但我真的不知道应该把它放在代码中的哪个位置。

Thanks for any help.谢谢你的帮助。

You only actually consume data from the scanner if input.hasNextDouble() is true .如果input.hasNextDouble()true则您实际上只使用来自扫描仪的数据。

Currently, if A == 0 and there are non-numeric data in the buffer then you'll indeed loop indefinitely.目前,如果A == 0并且缓冲区中有非数字数据,那么您确实会无限循环。

You need to consume data from the buffer on all control paths.您需要在所有控制路径上使用缓冲区中的数据。 In particular, if there is something non-numeric in the buffer, then you need to consume and immediately discard it: input.next();特别是,如果缓冲区中有非数字的东西,那么你需要消耗并立即丢弃它: input.next(); would be adequate.就足够了。

Seems like you just want to get the value of A which should not be equal to 0 .似乎您只想获得不应该等于 0 的 A 值。 Read comments阅读评论

double A=0;
do {   
    System.out.println("Set the A param: ");
    if(input.hasNextDouble() == true) { //check for valid value 
        A = input.nextDouble();
        if(A == 0) {
            System.out.println("Param A cannot be a 0!");
        }
    } else{ // no valid value found , print msg and jump over the previous input
        input.nextLine();
        System.out.println("Param A must be a number!");
    }
} while(A == 0); // just check , if the desired value is received 
                 // previously input.hasNextDouble() had no use cuz we already              
                 // checked no double value found so will be false, don't use it 

First, you should not write the opposing check in an else if .首先,您不应该在else if写入相反的检查。 Just use else .只需使用else And you shouldn't check == true , since the value is already a boolean.而且您不应该检查== true ,因为该值已经是布尔值。

Now, for you infinite loop problem, when hasNextDouble() is false, it means that the user entered something wrong (ignoring the potential end-of-stream issue).现在,对于您的无限循环问题,当hasNextDouble()为 false 时,这意味着用户输入错误(忽略潜在的流结束问题)。 In that case you need to discard that bad input, which is best done by calling nextLine() .在这种情况下,您需要丢弃错误的输入,最好通过调用nextLine()

Java naming convention states that variables should start with lowercase letter, so A should be a . Java命名约定规定的变量应小写字母开头,这样A应该是a

Your code then becomes:然后您的代码变为:

double a = 0;
do {   
    System.out.println("Set the A param: ");
    if (input.hasNextDouble()) {
        a = input.nextDouble();
        if (a == 0) {
            System.out.println("Param A cannot be a 0!");
        }
    } else {
        input.nextLine(); // discard bad input
        System.out.println("Param A must be a number!");
    }
} while (a == 0);

Thank you for both answers, after first I wrote this:感谢您的两个答案,首先我写了这个:

        do
        {   

            System.out.println("Podaj wartość parametru A: ");

            if(input.hasNextDouble() == true)
            {
                A = input.nextDouble();
                if(A == 0)
                {
                    System.out.println("Parametr A nie może być zerem!");
                }
            }
            else if(input.hasNextDouble() == false)
            {
                System.out.println("Parametr A musi być liczbą!");
                input.nextLine();
                A = 0;
            }

        } while(A == 0);

And it worked but thanks to the second answer now I know how to do it better.它有效,但多亏了第二个答案,我现在知道如何做得更好。 :) Thanks both of you once again. :) 再次感谢你们俩。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM