【问题标题】:Switch case increment only adding once to array切换案例增量仅添加一次到数组
【发布时间】:2021-12-25 20:00:58
【问题描述】:

我必须从用户那里获得一个文本输入,然后我必须将每个元音 a,e,i,o,u 替换为 1,2,3,4,5 并显示每个元音的替换数量和然后还有更改的文本。 问题是最后的文本看起来很好替换,但替换的数量显示为 1。

(我尝试在下面计算“myText”中的 1、2、3、4 和 5,它运行良好,但如果用户输入数字,它也会计算在内,所以这是一个问题)

这是第一部分:

public static void replaceAndCount(String myText, int[] vowels) {

    for (int i = 0; i < myText.length(); i++) {            
        switch (myText.charAt(i)) {
            case 'a':
                vowels[0]++;
                myText = myText.replace('a', '1');              
            case 'e':
                vowels[1]++;
                myText = myText.replace('e', '2');                    
            case 'i':
                vowels[2]++;
                myText = myText.replace('i', '3');                    
            case 'o':
                vowels[3]++;
                myText = myText.replace('o', '4');                    
            case 'u':
                vowels[4]++;
                myText = myText.replace('u', '5');
        }                  
    }
}

【问题讨论】:

  • 我在 for 之后和 switch 之前使用 char[] myTextChar = myText.toCharArray() 修复了它,然后在机箱内部使用 myTextChar[i] = '1'; 并在我使用 myText = String.valueOf(myTextChar); 的开关之后使用了myText = String.valueOf(myTextChar);

标签: java replace switch-statement iteration increment


【解决方案1】:

希望下面的代码对你有用

代码:

public class ReplaceVowels {

public static void main(String[] args) {
    String myText = "aaaeiou";
    int[] vowels = new int[5];
    replaceAndCount(myText.toLowerCase(), vowels);
}

private static void replaceAndCount(String text, int[] vowels) {
    StringBuilder myText = new StringBuilder(text);
    for (int i = 0; i < myText.length(); i++) {
        switch (myText.charAt(i)) {
            case 'a':
                vowels[0]++;
                myText.setCharAt(i, '1');
                break;
            case 'e':
                vowels[1]++;
                myText.setCharAt(i, '2');
                break;
            case 'i':
                vowels[2]++;
                myText.setCharAt(i, '3');
                break;
            case 'o':
                vowels[3]++;
                myText.setCharAt(i, '4');
                break;
            case 'u':
                vowels[4]++;
                myText.setCharAt(i, '5');
        }
    }
    for (int vowel : vowels) {
        System.out.println(vowel);
    }
    System.out.println(myText);
}

结果

替换单个字符可以参考this

【讨论】:

    【解决方案2】:

    您的代码中有 2 个问题。

    1. 你必须在每种情况下都使用break,否则匹配情况下的所有情况都会被执行。例如,
    case 'a':
        vowels[0]++;
        myText = myText.replace('a', '1'); 
        break;          
    case 'e':
        vowels[1]++;
        myText = myText.replace('e', '2');
        break;
    
    1. 字符串replace(old,new) 会将所有出现的旧字符替换为新字符。这就是为什么,替换数量显示为 1
      请参阅此string replacement at specific index

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多