【问题标题】:Unreachable Statement Error; No Infinite Loops or Return Statements Prior to the point of issue无法访问的语句错误;在问题点之前没有无限循环或返回语句
【发布时间】:2021-09-30 04:26:16
【问题描述】:

以下两行代码出现无法访问的语句错误:

System.out.println("We Made It!");

和

                switch (runs[i].charAt(2)) {
                    case 'A':
                        tempData.add((byte) 0xA); //0b1010);
                        continue;
                        //if hexadecimal is 11 concatenate an "a" to the string
                    case 'B':
                        tempData.add((byte) 0xB);//0b1011);
                        continue;
                        //if hexadecimal is 12 concatenate an "a" to the string
                    default:
                        continue;

我怀疑问题在于 switch 语句之后的代码无法访问。在 else 语句中的第一个语句之后如何让无法访问的 switch 语句执行?

这是整个方法:

public static byte[] stringToRle(String rleString) {
    //variable declarations
    String[] runs = rleString.split(":");
    ArrayList<Byte> tempData = new ArrayList<Byte>();

    //start of for loop
    //iterate through every element of the loop
    for (int i = 0; i < runs.length; i++) {
        //if the string in the array has a length of 2, execute the following code
        if (runs[i].length() == 2) {
        //execute the switch statement twice, once for each letter of the two letter string    
            for(int j = 0; j < runs[i].length(); j++) {
                switch (runs[i].charAt(j)) {
                    case '0':
                        tempData.add((byte) 0x0);
                        continue;
                    case '1':
                        tempData.add((byte) 0x1);
                        continue;
                    case '2':
                        tempData.add((byte) 0x2);
                        continue;
                    default:
                        continue;
                }
            }
        }
        //if the string in the array has a length of 3, execute the following code
        else if(runs[i].length() == 3) {
                //the following code executes fine
                switch (runs[i].charAt(1)) {
                    case '0':
                        tempData.add((byte) 0xA);
                        continue;
                    case '1':
                        tempData.add((byte) 0xB);
                        continue;
                    case '2':
                        tempData.add((byte) 0xC);
                        continue;
                    default:
                        continue;
                }
                //FIRST UNREACHABLE STATEMENT
                //FIRST UNREACHABLE STATEMENT
                //FIRST UNREACHABLE STATEMENT
                //FIRST UNREACHABLE STATEMENT
                //FIRST UNREACHABLE STATEMENT
                switch (runs[i].charAt(2)) {
                    case 'A':
                        tempData.add((byte) 0xA); //0b1010);
                        continue;
                        //if hexadecimal is 11 concatenate an "a" to the string
                    case 'B':
                        tempData.add((byte) 0xB);//0b1011);
                        continue;
                        //if hexadecimal is 12 concatenate an "a" to the string
                    default:
                        continue;
                }
            //SECOND UNREACHABLE STATEMENT
            //SECOND UNREACHABLE STATEMENT
            //SECOND UNREACHABLE STATEMENT
            //SECOND UNREACHABLE STATEMENT
            //SECOND UNREACHABLE STATEMENT
            System.out.println("We Made It!");
        }
    }
    byte[] array = new byte[tempData.size()];
    for(int i = 0; i < array.length; i++) {
        array[i] = tempData.get(i);
        System.out.println(" " + array[i]);
    }
    return array;
}

【问题讨论】:

    标签: java switch-statement


    【解决方案1】:

    continue for for 循环。当您将它与switch 案例一起使用时。因此,当您在每个 switch 案例中放置 continue 时,它​​会继续 for 循环。下面的代码根本不会执行。因为 switch 中的每一个 continue 实际上都是在重新开始循环。

    看来您需要在 switch 案例中使用 break 而不是 continue 才能破案。

    【讨论】:

    • @ScaryWombat Continue 如果被 for 包围肯定会起作用。我的意思是,如果我们在每个 switch 条件中放置 continue (如代码中所做的那样),它将导致在 switch 语句之后的 for 循环内无法访问代码。这就是这里发生的事情。
    • 知道了。我在看无限循环代码。
    • 对不起,不是我。
    • 我明白你的意思,“继续”在循环顶部启动下一次迭代,而不运行“继续”语句下面的代码。谢谢!
    猜你喜欢
    • 2016-09-27
    • 2011-05-28
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多