检查字符是否为元音使用 switch-case

huangapple go评论69阅读模式
英文:

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 &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;, &#39;A&#39;, &#39;E&#39;, &#39;I&#39;, &#39;O&#39;, &#39;U&#39;, &#39;Y&#39;:
f += 1;
ind += 1;
break;
//in case it&#39;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&#39;s a special character, the code will pass to the next char
case &#39;`&#39;, &#39;~&#39;, &#39;!&#39;, &#39;@&#39;, &#39;#&#39;, &#39;$&#39;, &#39;%&#39;, &#39;^&#39;, &#39;&amp;&#39;, &#39;*&#39;, &#39;-&#39;, &#39;_&#39;, &#39;+&#39;, &#39;=&#39;, &#39;\\&#39;, &#39;|&#39;, &#39;;&#39;, &#39;:&#39;, &#39;,&#39;, &#39;.&#39;, &#39;/&#39;, &#39;?&#39;:
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 &lt; sentence.length()) {
char letter = sentence.charAt(ind);
switch (letter) {
case &#39;a&#39;, &#39;A&#39;, &#39;e&#39;, &#39;E&#39;, &#39;i&#39;, &#39;I&#39;, &#39;o&#39;, &#39;O&#39;, &#39;u&#39;, &#39;U&#39;, &#39;y&#39;, &#39;Y&#39;:
f += 1;
break;
case &#39;b&#39;, &#39;B&#39;, &#39;c&#39;, &#39;C&#39;, &#39;d&#39;, &#39;D&#39;, &#39;f&#39;, &#39;F&#39;, &#39;g&#39;, &#39;G&#39;, &#39;h&#39;, &#39;H&#39;, 
&#39;j&#39;, &#39;J&#39;, &#39;k&#39;, &#39;K&#39;, &#39;l&#39;, &#39;L&#39;, &#39;m&#39;, &#39;M&#39;, &#39;n&#39;, &#39;N&#39;, &#39;p&#39;, &#39;P&#39;, 
&#39;q&#39;, &#39;Q&#39;, &#39;r&#39;, &#39;R&#39;, &#39;s&#39;, &#39;S&#39;, &#39;t&#39;, &#39;T&#39;, &#39;v&#39;, &#39;V&#39;, &#39;w&#39;, &#39;W&#39;, 
&#39;x&#39;, &#39;X&#39;, &#39;z&#39;, &#39;Z&#39;:
g += 1;
break;
default:
break;
}
ind += 1;
}

huangapple
  • 本文由 发表于 2020年10月2日 04:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64162519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定