英文:
Check if char is a vowel using switch-case
问题
//f和g - 元音和辅音计数
int f, g, ind;
f = 0;
g = 0;
ind = 0;
char letter = sentence.charAt(ind);
while (ind < sentence.length()) {
letter = sentence.charAt(ind);
switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
f += 1;
ind += 1;
break;
//如果是数字,则代码将跳至下一个字符
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ind += 1;
break;
//如果是特殊字符,则代码将跳至下一个字符
case '`':
case '~':
case '!':
case '@':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '-':
case '_':
case '+':
case '=':
case '\\':
case '|':
case ';':
case ':':
case ',':
case '.':
case '/':
case '?':
ind += 1;
break;
default:
g += 1;
ind += 1;
break;
}
}
英文:
I'm trying to check if each character in a string using switch a statement inside a while loop. So far I have this:
//f and g - vowel and consonant count
int f, g, ind;
f = 0;
g = 0;
ind = 0;
char letter = sentence.charAt(ind);
while (letter != sentence.charAt(ind)) {
switch (letter) {
case 'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y':
f += 1;
ind += 1;
break;
//in case it's a number, the code will pass to the next char
case 1, 2, 3, 4, 5, 6, 7, 8, 9:
ind += 1;
break;
//in case it's a special character, the code will pass to the next char
case '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '+', '=', '\\', '|', ';', ':', ',', '.', '/', '?':
ind += 1;
break;
default:
g += 1;
ind += 1;
break;
}
}
For some reason it just returns 0 for both variables. Any suggestions? I'm to this btw so don't be too mean (a little bit is fine if I did something dumb). Also, if anyone has a more efficient way to check if the char is a special character, that would be much appreciated. Thanks in advance.
答案1
得分: 1
变量 letter
从不改变,因此您始终测试相同的字符。您的 while 循环将在达到与第一个字符不同的第一个字符时停止,因此它会很快停止。
顺便说一下,您可以简化您的代码,直接调用辅音:
int f = 0, g = 0; // 元音和辅音计数器
int ind = 0; // 字符索引
while (ind < sentence.length()) {
char letter = sentence.charAt(ind);
switch (letter) {
case 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y':
f += 1;
break;
case 'b', 'B', 'c', 'C', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H',
'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'p', 'P',
'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'v', 'V', 'w', 'W',
'x', 'X', 'z', 'Z':
g += 1;
break;
default:
break;
}
ind += 1;
}
英文:
The variable letter
never changes, so you always test the same character. Your while loop will stop when it reaches the first character that is different from the first one, so it will stop soon.
By the way, you may lighten your code calling consonants directly:
int f = 0, g = 0; // vowel and consonant counters
int ind = 0; // character index
while (ind < sentence.length()) {
char letter = sentence.charAt(ind);
switch (letter) {
case 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y':
f += 1;
break;
case 'b', 'B', 'c', 'C', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H',
'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'p', 'P',
'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'v', 'V', 'w', 'W',
'x', 'X', 'z', 'Z':
g += 1;
break;
default:
break;
}
ind += 1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论