感觉这里的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
自 if
和 else 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/