我觉得这里有太多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!");
}
}
答案 0 :(得分:2)
移动否定价格检查
将第itemPrice < 0
块中的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;
}
答案 1 :(得分:1)
您可以先检查所有负面情况,然后再进一步简化。
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 < 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 if (itemPrice == budgetRemaining) {
budgetRemaining = 0;
System.out.println("You have reached your maximum budget. Thank you for shopping with us!");
} else {
budgetRemaining -= itemPrice;
}
}
}
答案 2 :(得分:0)
这是我的代码
while (budgetRemaining > 0) {
System.out.println("You have a remaining budget of $" + budgetRemaining + ". Please enter price of item:");
itemPrice = enterPrice.nextDouble();
if (itemPrice < 1){
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;
}
}
System.out.println("You have reached your maximum budget. Thank you for shopping with us!");
我在从预算中删除项目的值之前检查条件是否为真。如果我知道itemPrice有效:我只进行减法。
此外,当您退出货币时,while循环将自动退出,因此无需在实际退出前检查余额。你不妨打印一下这句话。
我做的另一件小事是设置itemPice&lt; 1,因为我认为该项目不能完全免费。