Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用扫描仪验证用户输入_Java_Validation_Input_Java.util.scanner_User Input - Fatal编程技术网

Java 使用扫描仪验证用户输入

Java 使用扫描仪验证用户输入,java,validation,input,java.util.scanner,user-input,Java,Validation,Input,Java.util.scanner,User Input,我很难弄清楚如何正确使用扫描仪来验证用户输入。例如,程序需要用户输入int,用户只能输入正数,如果用户输入“apple”、“deliciousapple”或负数,问题将显示错误消息。我尝试了下面的代码,然后我发现了一个令人不安的问题,“那不是一个数字!”被打印了两次,我不知道是什么导致了这个问题 import java.util.*; public class input { public static void main(String[] args){ Scanner

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

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);
    }
}
我在“number=sc.nextInt()”下面加了一行代码,意外地解决了这个问题,现在我的代码变成:

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);
    }
}
这一次,“那不是一个数字”只打印了一次,但我真的不明白为什么放“sc.nextLine()”可以帮助解决问题


还有一个问题,如果当用户输入诸如“23 sdfd”、“23 2323”之类的内容时,我还想验证用户的输入,并且问题仍然提供提示用户重试的信息,直到他们输入正确的int?

这是因为
number=sc.nextInt()-2232
后,code>未使用换行符。所以它计算了那个换行符,确定它不是一个int,并提示“那不是一个数字!”。然后您输入了
dfd dfd
,它确定这不是一个整数,并再次提示“这不是一个数字!”

您的编辑通过在读取int后使用换行符修复了该问题


参见

一个“正数”只是一个正整数吗?例如,你不考虑双打吗?+ 1 @ AdEB,如果号码是“代码>长,而不是<代码> int >代码,它将不起作用。你似乎对你是在写代码还是在编写代码本身感到困惑。这是您的代码,您应该控制它的行为(在任何情况下)。对于第一个代码,当您输入-2232时,流将移出while循环。然后使用
nextline()
输入另一个值。在输入中输入了一个字符串。之后,在重新迭代do时再次输入另一个值。因此,
扫描仪
包含两个值。因此,输出将打印两次
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