c语言用switch判断元音,确定用户输入可以是一个元音或不使用switch语句

由于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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值