java - if-else 不应该有 break 吗?

标签 java while-loop break

所以我的教授提到 if/if-else 语句中的中断是“糟糕的”代码。 她到底是什么意思?另外,我如何才能修复我目前编写的代码,因为它确实按照我想要的方式工作,现在我需要摆脱 break 语句。

    int sumOne = 1;
    int sumTwo = 1;
    int sumOneTotal = 0;
    int sumTwoTotal = 0;
    while(sumOne > 0 || sumTwo > 0){
        System.out.print("Enter a number to add to first sum: ");
        //The user enters in a value for the first sum.
        sumOne = input.nextInt();

        /**
         * We use an if-else statment to ensure sumOne is never less than or equal to 0.
         * If it does it ends the program immediately and totals the sums.
         * This is because we only want the user to enter in positive numbers.
         */
        if (sumOne <= 0){
            break;
        }else{
            sumOneTotal = sumOneTotal + sumOne;
        }

        System.out.print("Enter a number to add to second sum: ");
        //The user enters in a value for the second sum.
        sumTwo = input.nextInt();

        /**
         * We use an if-else statment to ensure sumTwo is never less than or equal to 0. 
         * If it does it ends the program immediately and totals the sums.
         * This is because we only want the user to enter in positive numbers.
         */
        if (sumTwo <= 0){
            break;
        }else{
            sumTwoTotal = sumTwoTotal + sumTwo;
        }
    }
    //We print out the total of sumOneTotal and sumTwoTotal.
    System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);

本质上,我希望用户输入任何正数,然后将该数字添加到第一个或第二个总和中。一旦用户输入任何数字 <= 0 我希望程序立即停止。当我修改代码时,我一直遇到的问题是代码一直在运行。这意味着如果我让用户输入 0 以添加到第一个总和中,代码仍然要求用户为第二个总和输入一个数字。我需要它立即停止而不是继续。任何帮助都会有很大的帮助!我正在使用 Java。

编辑!!!所以假设我想制作一个程序来做我现在正在做的完全相同的事情,只是没有 break 语句。我该怎么做?一些规则。最外层的语句必须是一个“while”循环。它的内部运作可以是任何东西。我还需要机器打印出“输入一个数字以添加到第一个总和:”和“输入一个数字以添加到第二个总和:”交替。因此,如果我输入 1、2、3、4。第一个和是 4,第二个和是 6。最后的规则是它不能包含任何 break 语句!

最佳答案

这是对结构化编程还是新事物的倒退,回到 goto 语句等无处不在的时代。从理论上讲,理想情况下,您永远不必使用中断/继续,并且只有一个返回点。实际上,这样做可以使程序更难编写、更难阅读并占用更多计算资源,从而使您的工作变得更加困难。多次返回、继续和中断是真正结构化编程和意大利面条代码之间的中间人。使用得当,它们没有任何问题。

一般来说,我发现如果您已经在使用使您的代码难以阅读的不良做法(例如,编写大量逻辑 block 而不分解它、紧密耦合对象等),它们只会使您的代码模糊不清.

如果您有兴趣,here是关于为什么不使用它们的有趣观点的链接。和 here是关于它们为何有益的观点。

许多其他人已经用代码回答了,但这是我的镜头:)

public class Main {
    public static void main(String args[]) {
        int sumOne = 1;
        int sumTwo = 1;
        int sumOneTotal = 0;
        int sumTwoTotal = 0;
        Scanner input = new Scanner(System.in);
        while(sumOne > 0 || sumTwo > 0){
            System.out.print("Enter a number to add to first sum: ");
            sumOne = input.nextInt();
            if (is_positive(sumOne)){
                sumOneTotal = sum_numbers(sumOneTotal, sumOne);
                System.out.print("Enter a number to add to second sum: ");
                sumTwo = input.nextInt();
                if(is_positive(sumTwo)){
                    sumTwoTotal = sum_numbers(sumTwoTotal, sumTwo);
                }
            }
        }
        System.out.printf("%1s%1d%12s%1s%1d", "First sum: ", sumOneTotal, " ", "Second sum: ", sumTwoTotal);
        return;
    }
    public static int sum_numbers(int x, int y){
        int total = x + y;
        return total;
    }
    public static boolean is_positive(int x){
        boolean is_pos = true;
        if(x < 0){
            is_pos = false;
        }
        return is_pos;
    }
}

我会说它现在更难阅读了。我的代码越靠右,我就越为需要维护它的人感到难过。当然,我可以通过在方法中包装(更多)位来删除一两个级别的缩进。然后它变得更容易阅读,但有一点黑盒化每一点逻辑似乎都是多余的......

关于java - if-else 不应该有 break 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163788/

相关文章:

java - 我无法获取员工的平均年龄

java - Java中的数组一维右旋

python - 使用 while 循环是最佳实践时,python 中是否存在任何情况?

java - java中break的使用

css - 在元素之后/在网格 block 中插入分隔符或空格

java - 使用java将arraylist值插入sql server数据库

java - 如何从ubuntu终端启动JBoss 6.1.0服务器?

r - 打破 R 中的嵌套循环

Python:循环原始输入

python - while-true 循环内的输入提示