使用扫描仪验证用户输入 [英] Using Scanner to validate user input

查看:118
本文介绍了使用扫描仪验证用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何正确使用扫描仪来验证用户输入.例如,程序需要用户输入int,用户只能输入正数,如果用户输入"apple","delicious apple"或负数,则问题将显示错误消息.我尝试了下面的代码,然后发现一个令人不安的问题,那不是数字!"被打印两次,我不知道是什么引起了这个问题....

I have a hard time to figure out how to use the Scanner correctly to validate the user input. For example, the program need to have a user input of int, user can only input positive number, and if the user input "apple","delicious apple",or negative number, the problem will show error message. I have tried the code below, and then i found a disturbing questions, the "That's not a number!" are printed twice, I have no idea what caused this questions....

import java.util.*;
public class input {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2232
Please enter a positive number!
dfd dfd
That's not a number!
That's not a number!

然后我意外地通过在数字= sc.nextInt()"下面放置了额外的代码行来解决了这个问题,现在我的代码变成了:

And I accidently solve the issue by putting extra line of code below the "number = sc.nextInt()", and now my code became:

import java.util.*;

public class input {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();
            sc.nextLine();  //scan

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2323
Please enter a positive number!
dfd dfd
That's not a number!
fdfd 322
That's not a number!
23 sdfd
Thank you! The positive number is 23

这次,那不是数字"仅打印了一次,但是我真的看不到为什么通过放置"sc.nextLine()"来解决问题.....

this time the "That's not a number" printed only once, but I really can't see why by putting "sc.nextLine()" helps solves the problem.....

还有一个问题,如果我还想验证用户输入的内容,例如"23 sdfd","23 2323",该问题仍然提供提示用户重试的信息,该怎么办?直到他们输入正确的int?

and still, I come another question, what if I also want to validate the user input when they input something like "23 sdfd","23 2323" and the problem still provide that the information to prompt the user to try again until they input correct int?

推荐答案

这是因为number = sc.nextInt();输入-2232后没有使用换行符.因此,它评估了换行符,确定它不是整数,并提示这不是数字!".然后输入dfd dfd,它确定该整数不是整数,并提示这不是数字!"再次.

It's because number = sc.nextInt(); did not consume the newline character after you entered -2232. So it evaluated that newline character, determined that it's not an int, and prompted "That's not a number!". Then you entered dfd dfd, it determined that that was not an int, and prompted "That's not a number!" again.

您的编辑通过在读取int之后使用换行符来解决此问题.

Your edit fixed the problem by consuming the newline character after it read in the int.

请参见在使用next(),nextInt()或其他nextFoo之后,扫描仪会跳过nextLine() ()方法

这篇关于使用扫描仪验证用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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