我是这个网站的新成员,谢谢stackoverflwo 好吧,我有一个程序要做:
创建一个程序,找到并显示输入的字符是元音,辅音,数字还是特殊字符。
这是我的代码:
Scanner input = new Scanner(System.in);
System.out.print("Enter a value: ");
char num = input.nextLine().charAt(0);
if ((num >= 'A' && num <= 'Z') || (num >= 'a' && num <= 'z')) {
if (num == 'a' || num == 'e' || num == 'i' || num == 'o'
|| num == 'u' || num == 'A' || num == 'E' || num == 'I'
|| num == 'O' || num == 'U') {
System.out.println(" vowel");
} else {
System.out.println(" consonants");
}
} else if (num == '0' || num == '1' || num == '2' || num == '3'
|| num == '4' || num == '6' || num == '7' || num == '8'
|| num == '9') {
System.out.println(" digits");
}
else{
System.out.println("Special Character!");
}
我的问题是如何在条件语句中使用IgnoreCase来简化它?
if ((num >= 'A' && num <= 'Z') || (num >= 'a' && num <= 'z'))
and
if (num == 'a' || num == 'e' || num == 'i' || num == 'o'
|| num == 'u' || num == 'A' || num == 'E' || num == 'I'
|| num == 'O' || num == 'U')
是否可以制作一个包含IgnoreCase的新语句?因为我的代码看起来不简单而且很长
答案 0 :(得分:4)
答案 1 :(得分:1)
Maroun给出了答案,也许你可以使用
public static boolean isTestPassed(char num) {
return "0123456789AEIOUaeiou".indexOf(num) != -1; //
}