编写一个Java程序,使用if-else语句和一个示例来检查一个字符是元音还是辅音。If条件检查用户输入的字符是否是a、e、i、o、u、A、E、I、O、U。如果是,则是元音;否则,是辅音。
import java.util.Scanner;
public class CharVowelorConsonant1 {
private static Scanner sc;
public static void main(String[] args) {
char ch;
sc= new Scanner(System.in);
System.out.print("Please Enter any Character = ");
ch = sc.next().charAt(0);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch <= 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch <= 'O' || ch == 'U')
{
System.out.println(ch + " is Vowel");
}
else
{
System.out.println(ch + " is Consonant");
}
}
}

Java 程序使用 Switch Case 检查字符是元音还是辅音
请参考 If Else、Else If、Switch Case 和 ASCII Codes 来理解这个 Java 示例。
import java.util.Scanner;
public class Example2 {
private static Scanner sc;
public static void main(String[] args) {
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter any Character = ");
ch = sc.next().charAt(0);
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println(ch + " is Vowel");
break;
default:
System.out.println(ch + " is Consonant");
}
}
}
Please Enter any Character = g
g is Consonant
Please Enter any Character = U
U is Vowel
在这个查找字符是元音还是辅音的程序示例中;首先,我们检查给定字符的ASCII值是否等于65、69、73、79、85、97、101、105、111或117。因为它们的ASCII码属于大写和小写元音。
如果为真,它将打印元音语句。接下来,我们查找字符是否在65到90或97到122之间,以检查辅音。
import java.util.Scanner;
public class Example3 {
private static Scanner sc;
public static void main(String[] args) {
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter any Char = ");
ch = sc.next().charAt(0);
if(ch == 65 || ch == 69 || ch == 73 || ch == 79 || ch == 85 ||
ch == 97 || ch == 101 || ch == 105 || ch == 111 || ch == 117) {
System.out.println(ch + " is Vowel");
}
else if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)){
System.out.println(ch + " is Consonant");
}
else {
System.out.println("Please enter Valid Letter");
}
}
}
Please Enter any Char = k
k is Consonant
Please Enter any Char = 9
Please enter Valid Letter
Please Enter any Char = i
i is Vowel