由于vowel是您正在查看的字母,因此您需要将其添加到您的switch语句中。您提供的第一个代码不知道您用于比较的变量。
switch(vowel){ //You need something here.
case 'a':
case 'A':
// continue with other vowels
System.out.println("This is a Vowel:"+ vowel);
break;
default:
System.out.println("This is not a Vowel:"+ vowel);
break;
}
不要ch切换,你甚至不使用,在你的代码提供。除非您在代码中使用其他位置,否则可以将其彻底删除。
编辑
如果你想看看整个字符串并检查每个字符的它是否是一个元音尝试这样
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: "); //Better prompt IMO
String str = sc.next(); //Get the whole string
char[] myChar = str.toCharArray(); //Turn the string into an array of char
for (char c : myChar) { //For every char in the array
switch (c) { //Check if it is a vowel or not
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
System.out.println(c + " - Vowel"); //Easier for me to read
break;
default:
System.out.println(c);
break;
}
}
}