Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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中将000验证为3位数字?_Java - Fatal编程技术网

如何在java中将000验证为3位数字?

如何在java中将000验证为3位数字?,java,Java,我首先尝试将用户输入作为字符串接受,计算字符串的长度,然后将其转换为整数,因为我希望用户输入始终在000-999之间?但是除了000本身,其他的都可以很好地进行编码。我怎样才能做到这一点。任何帮助都将不胜感激。谢谢大家! System.out.println("Please enter first number: "); String UserEnter = in.nextLine(); if(UserEnter.length()==3){

我首先尝试将用户输入作为字符串接受,计算字符串的长度,然后将其转换为整数,因为我希望用户输入始终在000-999之间?但是除了000本身,其他的都可以很好地进行编码。我怎样才能做到这一点。任何帮助都将不胜感激。谢谢大家!

System.out.println("Please enter first number: ");
            String UserEnter = in.nextLine();
            if(UserEnter.length()==3){
                int UserInt1 = Integer.valueOf(UserEnter);
                if(UserInt1 >= 000 && UserInt1 <=999){
                    for(i = 0;i<=number;i++){ 
                        if(temp==array[i]||temp==array2[i]){ //compared temp with previous combination
                            display("Repeat");     
                            break;         
                        } else {
                            FirstNum = temp;
                        }
                    }
                } else {
                    System.out.println("The number must be between 000 and 999!");
                }
            } else {
                System.out.println("Invalid Input!Try again!");
            }
000等于0。因为您之前检查了长度为3,所以检查1、2、3、

if (UserInt1 >= 0)
但是,对于其余部分,由于代码不完整,很难判断您正在尝试做什么

System.out.println(001 == 1); // true

如果长度为3,则数字不能大于999,因此您只需检查>1即可


下面是一个使用regexp可以执行的操作示例:

private boolean validateUserInput(String value){
    Pattern pattern = Pattern.compile("(?!000)\\d{3}");

    Matcher matcher = pattern.matcher(value);

    return matcher.matches();
}
以及验证此功能的测试

@Test
public void test_regexp() {
    String noTAccepted = "000";
    assertThat(validateUserInput(noTAccepted)).isFalse();

    String accepted = "090";
    assertThat(validateUserInput(accepted)).isTrue();
}
我假设他们总是需要输入3位数字,比如090

--编辑

正如在评论中所说的,如果您想验证000,使用我的解决方案,只需将模式更改为\\d{3},然后所有内容都会通过

Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter first number: ");
    String UserEnter =scanner.nextLine();
    if(UserEnter.length()==3){
        int UserInt1 = Integer.valueOf(UserEnter);
        if(UserInt1 >= 000 && UserInt1 <=999){
            if(UserInt1==000){
                System.out.println(String.format("your number is between 000 and 999 
that is: %03d",0,0,UserInt1));
            }
            else
            System.out.println("your number is between 000 and 999 that is: 
"+UserInt1);
        }
    } else {
        System.out.println("Invalid Input!Try again! (NUMBER MUST BE BETWEEN 000 AND 
999)");
    }

这个答案对你有帮助。由于您希望将000显示为数字,可能这可以通过java.lang.String包中的String.format方法实现,因为您希望自己的字符串格式向用户显示所需的结果。

默认情况下,array/array2中没有0,对吗?不清楚temp、number、,array和array2我认为我们丢失了太多的代码,无法处理您试图执行的操作。为什么用户必须为0输入000?数字变量是什么?你能为这个添加更多的上下文吗?这样我们就可以更好地帮助你了。我希望你的代码能对000起作用,就像它对001-999范围内的其他字符串起作用一样。在使用000时,您会遇到什么错误或不希望出现的行为?下面是一个测试,显示000如何通过这两个条件:如果要将格式设置为000,请写入%03d,而不是%d%d%d!如果我的理解是正确的,OP希望接受000作为输入,请参阅标题以获得最清晰的示例,但他说他的代码没有,这显然是不可复制的
Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter first number: ");
    String UserEnter =scanner.nextLine();
    if(UserEnter.length()==3){
        int UserInt1 = Integer.valueOf(UserEnter);
        if(UserInt1 >= 000 && UserInt1 <=999){
            if(UserInt1==000){
                System.out.println(String.format("your number is between 000 and 999 
that is: %03d",0,0,UserInt1));
            }
            else
            System.out.println("your number is between 000 and 999 that is: 
"+UserInt1);
        }
    } else {
        System.out.println("Invalid Input!Try again! (NUMBER MUST BE BETWEEN 000 AND 
999)");
    }