所以我试图在 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();
}
我猜我可能已经在这个 bue 周围走了很长时间,任何帮助将不胜感激:)
谢谢