【问题标题】:Java Unreachable codeJava 无法访问的代码
【发布时间】:2016-09-10 23:34:39
【问题描述】:

为什么我的代码返回此错误?我犯了什么错误?我在这段代码中看不到我的错误。有人可以帮帮我吗?

代码如下:

    public static void minMax(int m[][])
    {
        int menor = 0;
        int maior = 0;
        int i = 0;
        int j = 0;
        int posicao = 0;

        for(i = 0; i < 4;i++)
        {
            for(j = 0; j < 5; j++)
            {
                if((i == 0) && (j == 0))
                {
                    menor = m[i][j];
                    posicao = i;
                }

                else
                {
                    if(m[i][j] < menor)
                    {
                        menor = m[i][j];
                        posicao = i;
                    }
                }
            }
        }

        for (i = posicao;;)
        {
            for(j = 0; j < 5; j++)
            {
                if(j == 0)
                {
                    maior = m[i][j];
                }

                else
                {
                    if(m[i][j] > maior)
                    {
                        maior = m[i][j];
                    }
                }
            }

        }

        System.out.println("\n\nThe smallest element of the array: " + menor);
        System.out.println("The line of the smallest element: " + posicao);
        System.out.printf("MINMAX element %d encountered at the position: [%d][%d]", maior, i, j);
    }

Eclipse 在这一行指出了错误:

System.out.println("\n\n数组的最小元素:" + menor);

【问题讨论】:

  • for (i = posicao;;)
  • for (i = posicao;;) 将导致无限循环,因此您永远无法访问println 代码。
  • 谢谢!我把这条线改成了一会儿(i == posicao){}

标签: java unreachable-code


【解决方案1】:

看起来你的 for 循环定义为上面的 for (i = posicao;;) 你的打印永远不会完成,没有退出案例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多