我需要用户输入:
- 没有数字
- 长度为 4 个字符
- 仅使用字母表中的某些字母 [R、B、G、P、Y、O]
我已经想出了如何不使用数字而只使用 4 个字符长度,但是,我似乎无法弄清楚如何限制字母表中的某些字母(除 R、B、G、P、Y、O 之外的所有字母。)
guess = input.nextLine();
guess = guess.toUpperCase();
while (guess.length() != 4 || guess.contains("[0-9]") || guess.contains("[ACDEFHIJKLMNQSTUVWXZ]")) {
System.out.println("Bad input! Try again");
System.out.println("Use the form \"BGRY\"");
guess = input.nextLine();
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码,它似乎不起作用
我有一个错误检查器,可确保用户输入的猜测
a) 具有特定大小(此有效)
b) 没有数字(无效)
c) 不使用某些字母(无效)
这里是我的代码,我不确定我做错了什么:
System.out.println("Enter Code:");
guess = input.nextLine();
guess = guess.toUpperCase();
while (guess.length() != v || guess.contains("[0-9]") || guess.contains("[ACDEFHIJKLMNQSTUVWXZ]")) {
System.out.println("Bad input! Try again");
System.out.println("Use the form \"BGRY\" with your respective length of letters");
guess = input.nextLine();
guess = guess.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)