我正在尝试检查一个单词是否仅包含一组字母,例如 I、O、S、H 和 X 假设用户输入:SSHX,输出将为是,但如果用户输入 SHEXX,则输出将为 NO
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.next();
word = word.toUpperCase();
int length = word.length();
char letter = 0;
for (int counter = 0; counter < length; counter++) {
letter = word.charAt(counter);
}
if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') {
System.out.print("NO");
} else {
System.out.print("YES");
}
}