虽然在不满足条件时循环不会中断

时间:2014-03-07 16:24:50

标签: java while-loop

程序应打印“数字11出现不止一次” 然后将duplicateFound设置为true并退出while循环,因为它是true。 但它也打印出“数字13不止一次出现”。

为什么会发生这种情况?我如何打破while循环,以便在找到第一个副本(11)后它不会继续运行? 这是一个效率问题,因为我只想知道是否存在重复。 我不在乎有多少。

public class DuplicateNumbers
{
    public static void main(String[] name)
    {
        int[] myArray = { 64, 17, 9,  55, 2, 11, 11, 1,  8, 23, 13, 13};
        boolean duplicateFound = false;
        while (duplicateFound == false) 
        {
            for(int i = 0; i < myArray.length; i++) 
            {           
                for(int j = i + 1; j < myArray.length; j++)
                {
                    if(myArray[j] == myArray[i])
                    {
                        System.out.println("The number " + myArray[i] + " occurs more than once.");
                        duplicateFound = true;
                    }
                }
            }
        }
        if(duplicateFound == false)
        {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

4 个答案:

答案 0 :(得分:2)

这是一个更清晰的方式来写这个。请注意,它删除了您可能认为需要while循环的内容。它在return循环中使用for语句而不是break,我认为这是一种在循环中处理结果的更简洁方法。

public class DuplicateNumbers
{
  private static boolean hasDuplicates(int[] myArray) {
    for(int i = 0; i < myArray.length; i++) 
    {           
      for(int j = i + 1; j < myArray.length; j++)
      {
        if(myArray[j] == myArray[i])
        {
           System.out.println("The number " + myArray[i] + " occurs more than once.");
           return true;
        }
      }
    }
    return false;
  }

  public static void main(String[] name)
  {
     int[] myArray = { 64, 17, 9,  55, 2, 11, 11, 1,  8, 23, 13, 13};
     if (!hasDuplicates(myArray)) {
        System.out.println("There are no duplicates.");
    }
    System.out.println("Array length is " + myArray.length);
  }
}

答案 1 :(得分:1)

使用break离开循环。因为你有嵌套循环,你需要命名循环和命名的中断:

评论:您不需要While循环。如果没有找到重复,你不想再次重复所有数组。

 outerFor: for(int i = 0; i < myArray.length; i++) 
 {           
    for(int j = i + 1; j < myArray.length; j++)
    {
        if(myArray[j] == myArray[i])
        {
           System.out.println("The number " + myArray[i] + " occurs more than once.");
           duplicateFound = true;
           break outerFor;
        }
    }
  }

其实你根本不需要休息。当你在一开始遇到重复时,它只会节省一些计算时间。

答案 2 :(得分:1)

问题是你的for循环不会在duplicateFound条件下进行测试。如果要在发生重复时退出每个循环,可以执行以下操作:

public class DuplicateNumbers {
    public static void main(final String[] name) {
        final int[] myArray = { 64, 17, 9, 55, 2, 11, 11, 1, 8, 23, 13, 13 };
        boolean duplicateFound = false;
        while (duplicateFound == false) {
            for (int i = 0; i < myArray.length && !duplicateFound; i++) {
                for (int j = i + 1; j < myArray.length && !duplicateFound; j++) {
                    if (myArray[j] == myArray[i]) {
                        System.out.println("The number " + myArray[i]
                                + " occurs more than once.");
                        duplicateFound = true;
                    }
                }
            }
        }
        if (duplicateFound == false) {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

另一种选择是使用label和break语句,尽管通常我不是这种非结构化代码的忠实粉丝。

例如:

public class DuplicateNumbers {
    public static void main(final String[] name) {
        final int[] myArray = { 64, 17, 9, 55, 2, 11, 11, 1, 8, 23, 13, 13 };
        boolean duplicateFound = false;
        whileLoop: while (duplicateFound == false) {
            for (int i = 0; i < myArray.length; i++) {
                for (int j = i + 1; j < myArray.length; j++) {
                    if (myArray[j] == myArray[i]) {
                        System.out.println("The number " + myArray[i]
                                + " occurs more than once.");
                        duplicateFound = true;
                        break whileLoop;
                    }
                }
            }
        }
        if (duplicateFound == false) {
            System.out.println("There are no duplicates.");
        }
        System.out.println("Array length is " + myArray.length);
    }
}

答案 3 :(得分:0)

一旦找到副本,您应该break;

System.out.println("The number " + myArray[i] + " occurs more than once.");
duplicateFound = true;
break;