我已经修复了我的代码,以便它识别他们的 4 位数和更少或 6 位数和更高,但现在我想知道它是否包含数字中的字母。
下面的代码检测字母并仅在我输入 5 个字母时打印我想要的行,并且我希望它检测它们的数字多于字母或字母多于数字。
String digit;
String regex;
String regex1;
regex = "[0-9]{5}";
regex1 = "^[a-zA-Z0-9]{5}";
String test;
String validLength= "5";
char one, two, three, four, five;
{
System.out.println("In this game, you will have to input 5 digits.");
do
{
System.out.println("Please input 5-digits.");
digit = console.next();
test = digit.replaceAll("[a-zA-Z]", "");
if (digit.matches(regex))
{
one = (char)digit.charAt(0);
two = (char)digit.charAt(1);
three = (char)digit.charAt(2);
four = (char)digit.charAt(3);
five = (char)digit.charAt(4);
System.out.println((one + two + three + four + five) / 2 );
}
else if (test.length() > 5 || test.length() < 5)
{
System.out.println("You have letters in there.");
}
else if (digit.matches(regex1))
{
test = digit.replaceAll("[a-zA-Z]", "");
System.out.println("You have letters in there.");
}
else
if (digit.length() < 5)
{
System.out.println("You don't have enough digits.");
}
else if (digit.length() > 5)
{
System.out.println("You have to many digits.");
}
} while (!digit.matches(regex));