繁体   English   中英

数组中两个整数之和不为零时如何退出循环并打印语句

[英]How to exit the loop and print a statement when the sum of two integers in an array is not equal to zero

我正在尝试存储和打印给定数组中两个整数之和为零的数组。 但是当没有两个整数之和为零时,我无法退出循环并打印“没有两个整数之和为零”。 请让我知道如何解决这个问题。

public static void main(String[] args) {
        int[] origArr = { 1, 2, 3, -6, 8, 4 };
        for (int i = 0; i < origArr.length; i++) {
            int firstNumber = origArr[i];
            for (int j = i + 1; j < origArr.length; j++) {
                int secondNumber = origArr[j];
                if (firstNumber + secondNumber == 0) {
                    int[] newArr = new int[2];
                    newArr[0] = firstNumber;
                    newArr[1] = secondNumber;
                    System.out.println(Arrays.toString(newArr));

                }

                else {

                    System.out.println("There are no two integers whose sum is zero ");

                }

            }

        }

    }

当“If”条件不满足时,只要循环在迭代,系统就会多次打印 sopln。

如果没有几个整数符合您的条件,则意味着您已经检查了数组中的所有可能组合。

如果您检查了所有组合,则意味着两个循环都已终止。

“无解”打印应该在两个循环之后完成。 要知道是否需要打印它,您可以使用布尔值。 找到任何匹配的组合时将其设置为 true:

public static void main(String[] args) {
    int[] origArr = { 1, 2, 3, -6, 8, 4 };
    boolean solutionFound = false;
    for (int i = 0; i < origArr.length-1; i++) {
        int firstNumber = origArr[i];
        for (int j = i + 1; j < origArr.length; j++) {
            int secondNumber = origArr[j];
            if (firstNumber + secondNumber == 0) {
                // set the flag to true
                solutionFound = true;
                System.out.println(firstNumber + ", " + secondNumber);
            }
        }
    }
    // check the flag to see if a solution was found
    if (!solutionFound) {
        System.out.println("There are no two integers whose sum is zero ");
    }
}

这是一个带有一些日志记录和数组中发现的正数的另一个:

public static void main(String[] args) {
    int[] origArr = { 1, 2, 3, -6, 8, 4 , -4 };
    boolean foundit = false;
    for (int i = 0; i < origArr.length; i++) {
        int firstNumber = origArr[i];
        for (int j = i + 1; j < origArr.length; j++) {
            int secondNumber = origArr[j];
            System.out.println(Integer.toString(firstNumber));
            System.out.println(Integer.toString(secondNumber));
            if (firstNumber + secondNumber == 0) {
                int[] newArr = new int[2];
                newArr[0] = firstNumber;
                newArr[1] = secondNumber;
                System.out.println(Arrays.toString(newArr));
                System.out.println("Found !!!!!");
                foundit = true;
                }
            }
        }
        if (foundit == false) {
            System.out.println("There are no two integers whose sum is zero ");
        }
    }
}

以下stream解决方案演示:

  • 寻找解决方案的不同方法。
  • 以及在没有时正确打印。

它基本上使用一对Intstreams进行嵌套循环,并创建一个包含要测试的值的数组流。 flatMap将流的流合并为一个,然后根据数组元素的总和过滤数组。 然后将它们收集到一个list 然后打印所有解决方案。 如果列表为空,则打印适当的消息。

        int len = origArr.length;
        List<int[]> list = IntStream.range(0, len - 1)
            .mapToObj(i -> IntStream.range(i + 1, len)
                .mapToObj(j -> new int[] { origArr[i], origArr[j] }))
            .flatMap(Function.identity())
            .filter(a -> a[0] + a[1] == 0)
            .collect(Collectors.toList());

        if (list.isEmpty()) {
            System.out.println(
                    "There are no two integers whose sum is zero ");
        } else {
            list.forEach(arr -> System.out.println(Arrays.toString(arr)));
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM