如何检查字符是否是元音? [英] How do I check if a char is a vowel?
问题描述
这段Java代码给我带来麻烦:
This Java code is giving me trouble:
String word = <Uses an input>
int y = 3;
char z;
do {
z = word.charAt(y);
if (z!='a' || z!='e' || z!='i' || z!='o' || z!='u')) {
for (int i = 0; i==y; i++) {
wordT = wordT + word.charAt(i);
} break;
}
} while(true);
我想检查字的第三个字母是否为非元音,如果是希望它返回非元音及其前面的任何字符。如果它是元音,它检查字符串中的下一个字母,如果它也是元音,那么它检查下一个字母,直到它找到一个非元音。
I want to check if the third letter of word is a non-vowel, and if it is I want it to return the non-vowel and any characters preceding it. If it is a vowel, it checks the next letter in the string, if it's also a vowel then it checks the next one until it finds a non-vowel.
示例:
word = Jaemeas then wordT must = Jaem
word = Jaemeas then wordT must = Jaem
示例2:
word = Jaeoimus then wordT must = Jaeoim
word=Jaeoimus then wordT must =Jaeoim
问题是我的如果
语句,我不知道如何使它检查所有元音在一行。
The problem is with my if
statement, I can't figure out how to make it check all the vowels in that one line.
推荐答案
您的情况有缺陷。想想更简单的版本
Your condition is flawed. Think about the simpler version
z != 'a' || z != 'e'
如果 z
是'a'
那么下半部分将是真的,因为 z
不是'e'
(即整个条件为真),如果 z
为'e'
上半部将是真的,因为 z
不是'a'
(同样,整个条件为真)。当然,如果 z
既不是'a'
也不是'e'
,那么这两个部分都是真的。换句话说,你的条件永远不会是假的!
If z
is 'a'
then the second half will be true since z
is not 'e'
(i.e. the whole condition is true), and if z
is 'e'
then the first half will be true since z
is not 'a'
(again, whole condition true). Of course, if z
is neither 'a'
nor 'e'
then both parts will be true. In other words, your condition will never be false!
你可能想要&&
z != 'a' && z != 'e' && ...
或者:
"aeiou".indexOf(z) < 0
这篇关于如何检查字符是否是元音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!