在java Scanner.next()中 [英] In java Scanner.next()

查看:57
本文介绍了在java Scanner.next()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Calculator
{

    public static void main(String[] args)
    {
        boolean isValid = false;
        Scanner myScanner = new Scanner(System.in);
        String customerType = null;
        System.out.print("Customer Type? (C/R) ");
        customerType = myScanner.next();
        while (isValid == false)
        {
            System.out.print("Enter Subtotal: ");

            if (myScanner.hasNextDouble())
            {
                double sobTotal = myScanner.nextDouble();
                isValid = true;
            }
            else
            {
                System.out
                        .println("Hay! Entry error please enter a valid number");
            }
            myScanner.nextLine();
        }
    }
}

我是Java的新手,像往常一样是在Scanner类中尝试了一些东西.

Hi, im new to java, as usual im trying out a few things in the Scanner Class.

有没有办法查看扫描仪的输入? 因为我在上面的代码中遇到了问题,如您所见.这是我输入错误数据后控制台窗口的输出.而不是我输入KKK的数字,所以有人可以向我解释为什么我两次收到此错误消息吗?

is there a way to see the input of the scanner? Cuz i have a problem here with the code above as you'll see. this is the output of my console window after i entered wrong data. instead of numbers i entered KKK so can somebody explain me why i got this error message 2 times?

"this is the console" 
Customer Type? (C/R) R
Enter Subtotal: KKK
Hay! Entry error please enter a valid number
Enter Subtotal: Hay! Entry error please enter a valid number
Enter Subtotal: 

推荐答案

问题是您正在调用scan.nextdouble(),当输入中有双精度字时,它将正常工作.当输入中没有双精度数时,对nextDouble()的调用将忽​​略输入为"KKK"的输入,并显示错误消息,然后,当您调用nextLine()时,相同的输入"KKK"仍然在那里等待您将循环返回"KKK",然后将其传递回您的程序,由于它仍然不是两倍,因此您会收到重复的错误消息.

The problem is you're calling scanner.nextdouble() which will work fine if and when there is a double in the input. When there is not a double in the input your input that was "KKK" is ignored by the call to nextDouble() and your error message is displayed then when you call nextLine() that same input "KKK" is still there waiting wo when you loop back throught the while the "KKK" is then passed back to your program and since it is still not a double you get the repeated error message.

尝试一下:

    boolean isValid = false;
    Scanner myScanner = new Scanner(System.in).useDelimiter("\\n");
    String customerType = null;
    System.out.print("Customer Type? (C/R) ");
    customerType = myScanner.next();
    while (!isValid)
    {
        System.out.print("Enter Subtotal: ");

        if (myScanner.hasNextDouble())
        {
            double sobTotal = myScanner.nextDouble();
            isValid = true;
        }
        else
        {
            System.out.println("Hay! Entry error please enter a valid number");

            if(myScanner.hasNext()){
              myScanner.next();
            }
        }
    }

这将消耗无效的"KKK"输入,并使程序正常运行.

That will consume the invalid input of "KKK" and let the program continue properly.

这篇关于在java Scanner.next()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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