問題描述
這段 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 然后 wordT 必須 = Jaem
word = Jaemeas then wordT must = Jaem
示例 2:
word=Jaeoimus 然后 wordT 必須 =Jaeoim
word=Jaeoimus then wordT must =Jaeoim
問題在于我的 if
語句,我不知道如何讓它檢查那一行中的所有元音.
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
這篇關于如何檢查字符是否為元音?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!