java - 有没有进一步的方法来简化这段代码?

标签 java simplify

感觉这里的if语句太多了,有什么办法可以进一步简化吗?我确实减少了一点,但也许有更有效的解决方案?提前致谢!

Scanner enterPrice = new Scanner(System.in);
double budgetRemaining = 100, itemPrice;

while (budgetRemaining > 0) {
    System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
    System.out.println(itemPrice = enterPrice.nextDouble());

    if (itemPrice < budgetRemaining) {
        budgetRemaining -= itemPrice;

        if (itemPrice < 0) {
            budgetRemaining += itemPrice;
            System.out.println("Sorry, you have entered an invalid amount. ");
        }
    }
    else if (itemPrice > budgetRemaining) {
        System.out.println("Sorry, your item exceeds your budget.");
    }

    if (itemPrice == budgetRemaining) {
        budgetRemaining = 0;
        System.out.println("You have reached your maximum budget. Thank you for shopping with us!");
    }
}    

最佳答案

移动负价格检查

吊起itemPrice < 0 checkout 第一if堵塞。该错误检查应该出现在所有代码路径上,而不仅仅是第一个。在从预算中减去之前检查负价格将使您不必重新添加它。

while (budgetRemaining > 0) {
    System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
    itemPrice = enterPrice.nextDouble();

    if (itemPrice < 0) {
        System.out.println("Sorry, you have entered an invalid amount. ");
        continue;
    }

    ...
}

合并案例

然后我会结合 <==个案。保持逻辑尽可能相似:总是减去 itemPrice .唯一的区别是您在完成后打印一条消息。由于您有一个循环条件检查,您可以将最终打印输出移到循环之外并删除 if (itemPrice == budgetRemaining)完全检查。

while (budgetRemaining > 0) {
    System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
    itemPrice = enterPrice.nextDouble();

    if (itemPrice < 0) {
        System.out.println("Sorry, you have entered an invalid amount. ");
        continue;
    }

    if (itemPrice <= budgetRemaining) {
        budgetRemaining -= itemPrice;
    }
    else if (itemPrice > budgetRemaining) {
        System.out.println("Sorry, your item exceeds your budget.");
    }
}

System.out.println("You have reached your maximum budget. Thank you for shopping with us!");

删除冗余 else if

ifelse if检查现在是直接对立的,第二个可以变成简单的 else .

if (itemPrice <= budgetRemaining) {
    budgetRemaining -= itemPrice;
}
else {
    System.out.println("Sorry, your item exceeds your budget.");
}

提前退出

不过,我会调换顺序,以便您先检查是否超出预算。因为我们有 itemPrice < 0预先检查,预先检查其他错误情况也是有意义的。

while (budgetRemaining > 0) {
    System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
    itemPrice = enterPrice.nextDouble();

    if (itemPrice < 0) {
        System.out.println("Sorry, you have entered an invalid amount. ");
        continue;
    }

    if (itemPrice > budgetRemaining) {
        System.out.println("Sorry, your item exceeds your budget.");
        continue;
    }

    budgetRemaining -= itemPrice;
}

System.out.println("You have reached your maximum budget. Thank you for shopping with us!");

这种错误检查方式 + 使用 continue 提前退出很容易看出常规情况是什么:budgetRemaining -= itemPrice语句现在位于任何条件之外。它已被提升到主代码路径。很明显,其他检查和打印输出是前提条件。

或者,您可以使用 if 来编写它/else链。两者都有效。现在它只是关于风格偏好。

if (itemPrice < 0) {
    System.out.println("Sorry, you have entered an invalid amount. ");
}
else if (itemPrice > budgetRemaining) {
    System.out.println("Sorry, your item exceeds your budget.");
}
else {
    budgetRemaining -= itemPrice;
}

关于java - 有没有进一步的方法来简化这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47855726/

相关文章:

java - 如何防止 Java 中的 SocketInputStream.socketRead0 挂起?

java - Eclipse 工作区中的 .recommenders 和 .metadata 是什么,它们应该在设备之间同步吗?

java - Facelets:如何将属性保存到 View bean 中?

jquery - 这个 jQuery 下拉脚本可以简化吗?

java.lang.OutOfMemoryError : Java heap space while initialising an array 错误

java - 在 Java 中,如何有效地从字节数组的开头和结尾修剪 0

sql - postgresql工作时间简化查询

excel - 使用相对引用

C++ 结束这个 8 位 for 循环的最佳方法是什么

python - 如何在Python中化简分数?