【问题标题】:Unreachable Code using Arrays, Can't figure out why使用数组无法访问的代码,不知道为什么
【发布时间】:2018-09-28 19:24:50
【问题描述】:

所以我正在使用冒泡排序为我的计算机编程课做一个关于数组的实验室。无论如何,代码已经完成并且应该可以工作,但是有一部分出现“无法访问代码”的错误,我不知道为什么。我在这里看不到问题。这是完整的代码,以便您识别问题。

public class MClab22
{
  public static void main(String[] args)
  {
    int[] houseNums = {23, 76, 15, 47, 14, 38, 52};
    System.out.print("The original sequence is: \n     ");
    for (int i = 0; 1 < houseNums.length; i++)
    {
      System.out.print(houseNums [i] + ", ");
    }
    System.out.println();
    SortEm(houseNums);
  }
  private static void SortEm (int [] ar)
  {
    int temp;
    for (int i = ar.length - 1; 1 > 0; i--)
    {
      for (int j = 0; j < i; j++)
      {
        if (ar[j] > ar[j + 1])
        {
          temp = ar[j];
          ar[j] = ar[j + 1];
          ar[j+1] = temp;
        }
      }
    }
    System.out.print("The new sequence is : \n   ");
    for (int i=0; 1 < ar.length; i++)
    {
      System.out.print (ar[i] + ", ");
    }
    System.out.println();
  }
}

'Unreachable code' 的问题出现在第 29 行,并且是显示“System.out.print("The new sequence is : \n ");" 的部分 如果可以的话,请帮忙,非常感谢:)

【问题讨论】:

  • 你最外层的循环条件是:1 &gt; 0 i > 0吗?
  • for (int i = ar.length - 1; 1 &gt; 0; i--) 你永远不会离开这个循环
  • 你的循环是无限的。
  • 这也不好:for (int i=0; 1 &lt; ar.length; i++)。我想你的意思是for (int i=0; i &lt; ar.length; i++)
  • 大部分情况下,这种组合很有效。仍然有一个问题,但选择的答案成功地解决了它。谢谢大家!!

标签: java arrays bubble-sort unreachable-code


【解决方案1】:

这样试试:

public class MClab22{

      public static void main(String[] args)
      {
        int[] houseNums = {23, 76, 15, 47, 14, 38, 52};
        System.out.print("The original sequence is: \n     ");
        for (int i = 0;i < houseNums.length; i++)
        {
          System.out.print(houseNums [i] + ", ");
        }
        System.out.println();
        SortEm(houseNums);
      }
      private static void SortEm (int [] ar)
      {
        int temp;
        for (int i = ar.length - 1; i > 0; i--)
        {
          for (int j = 0; j < i; j++)
          {
            if (ar[j] > ar[j + 1])
            {
              temp = ar[j];
              ar[j] = ar[j + 1];
              ar[j+1] = temp;
            }
          }
        }

        System.out.print("The new sequence is : \n   ");
        for (int i=0; i < ar.length; i++)
        {
          System.out.print (ar[i] + ", ");
        }
        System.out.println();
      }

}

实际上有3个问题。第一个问题是条件 1>0 的循环。这个永远是真的。其他 2 个问题是您的循环条件为 1

【讨论】:

  • 成功了,非常感谢!我不知道它为什么会起作用,但它确实起作用了,哈哈
  • 我添加了差异对不起
【解决方案2】:

来自Wikipedia

在计算机编程中,无法访问的代码是源代码的一部分 一个永远无法执行的程序,因为不存在 控制从程序其余部分到代码的流路径。

这部分会导致问题:

for (int i = ar.length - 1; 1 > 0; i--)

特别是:

1 > 0

因为它总是正确的,所以你有一个无限循环并且永远不会执行:

System.out.print("The new sequence is : \n   "); 

【讨论】:

  • 虽然还有 2 个无限循环
  • 是的,它们是,但是编译器是顺序的,所以它会在它发现的第一个问题上失败。
【解决方案3】:

我想你是说

for (int i = ar.length - 1; i > 0; i--)

而不是

for (int i = ar.length - 1; 1 > 0; i--)

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2014-04-29
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多