gpt4 book ai didi

java - 尝试验证字符串输入时陷入 while 循环

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:50 24 4
gpt4 key购买 nike

因此,我尝试以 DD/MM/YYYY 格式输入出生日期,输入的长度不能超过 10 个字符,并且我必须确保其格式正确,以避免用户输入类似 A51/1/1982 之类的内容,我认为至少据我所知,我已经正确编写了它:),但在检查输入时,我现在永远陷入了 while 循环

       System.out.println("Please enter your date of birth in the format DD/MM/YYYY");
dateOfBirth = scanner.nextLine(); //Read date of birth
dob0 = dateOfBirth.substring(0);// to check char 0 is between 0-3
dob1 = dateOfBirth.substring(1);// to check char 1 is between 0-9
dob2 = dateOfBirth.substring(2);// to check char 2 is between /
dob3 = dateOfBirth.substring(3);// to check char 3 is between 0-1
dob4 = dateOfBirth.substring(4);// to check char 4 is between 0-9
dob5 = dateOfBirth.substring(5);// to check char 5 is between /
dob6 = dateOfBirth.substring(6);// to check char 6 is between 1-2
dob7 = dateOfBirth.substring(7);// to check char 7 is between 0-9
dob8 = dateOfBirth.substring(8);// to check char 8 is between 0-9
dob9 = dateOfBirth.substring(9);// to check char 9 is between 0-9
dob = dateOfBirth.length(); //convert string to int to check length
while (dob !=10 || !dob0.matches("[0-3]+") || !dob1.matches("[0-9]+") || !dob2.matches("[/]+")
|| !dob3.matches("[0-1]+") || !dob4.matches("[0-9]+") || !dob5.matches("[/]+")
|| !dob6.matches("[1-2]+") || !dob7.matches("[0-9]+") || !dob8.matches("[0-9]+")
|| !dob9.matches("[0-9]+"))//check all values
{
System.out.println("Please make sure you enter your date of birth in the format DD/MM/YYYY");
dateOfBirth = scanner.nextLine();
}

我猜我可能已经在这个问题上走了很长一段路了,但如果有任何帮助,我们将不胜感激:)

谢谢

最佳答案

您不会重新评估新的用户输入。仅输入第一行。输入新输入后,整个 dob0= .. dob = stuff 也应该进入 while 循环。

关于java - 尝试验证字符串输入时陷入 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com