Java 有没有进一步的方法来简化这段代码?
我觉得这里面的if语句太多了,有没有办法进一步简化呢?我确实减少了一点,但也许有更有效的解决方案?提前谢谢Java 有没有进一步的方法来简化这段代码?,java,simplify,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
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!");
}
}
Scanner enterprise=新扫描仪(System.in);
双预算付款=100,项目价格;
同时(预算设置>0){
System.out.println(“您的剩余预算为$”+BudgetMaining+”。请输入项目的价格:”;
System.out.println(itemPrice=enterPrice.nextDouble());
如果(项目价格<预算价格){
预算付款-=项目价格;
如果(itemPrice<0){
预算付款+=项目价格;
System.out.println(“对不起,您输入的金额无效。”);
}
}
else if(项目价格>预算支出){
System.out.println(“对不起,您的物品超出了预算。”);
}
如果(itemPrice==预算支出){
预算支出=0;
System.out.println(“您已经达到了最高预算。感谢您与我们一起购物!”);
}
}
您可以先检查所有负面条件,以进一步简化它
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;
}
}
}
Scanner enterprise=新扫描仪(System.in);
双预算付款=100,项目价格;
同时(预算设置>0){
System.out.println(“您的剩余预算为$”+BudgetMaining+”。请输入项目的价格:”;
System.out.println(itemPrice=enterPrice.nextDouble());
如果(itemPrice<0){
System.out.println(“对不起,您输入的金额无效。”);
}else if(项目价格>预算支出){
System.out.println(“对不起,您的物品超出了预算。”);
}else if(itemPrice==budgetRemaining){
预算支出=0;
System.out.println(“您已经达到了最高预算。感谢您与我们一起购物!”);
}否则{
预算付款-=项目价格;
}
}
}
移动负价格检查
将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;
}
...
}
删除冗余否则,如果
由于if
和else-if
检查现在是完全相反的,第二个检查可以变成简单的else
if (itemPrice <= budgetRemaining) {
budgetRemaining -= itemPrice;
}
else {
System.out.println("Sorry, your item exceeds your budget.");
}
这是我的密码
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!");
while(BudgetMaining>0){
System.out.println(“您的剩余预算为$”+BudgetMaining+”。请输入项目的价格:”;
itemPrice=enterPrice.nextDouble();
如果(项目价格<1){
System.out.println(“对不起,您输入的金额无效。”);
}
else if(项目价格>预算支出){
System.out.println(“对不起,您的物品超出了预算。”);
}
否则{
预算付款-=项目价格;
}
}
System.out.println(“您已经达到了最高预算。感谢您与我们一起购物!”);
在从预算中删除项目的值之前,我正在检查条件是否为真。如果我知道itemPrice是有效的,我就进行减法运算
此外,当您缺钱时,while循环将自动退出,因此无需在实际退出之前检查余额。你不妨在事后打印这份声明
我做的另一件小事是将itemPice设置为<1,因为我假设该项不可能完全免费。如果您的代码已经在工作,并且您只想让几双眼睛检查您的工作代码,那么我们的姐妹站点是解决这个问题的更好的地方完全可以替换为
else
,因为您已经声明物料价格不低于或高于剩余预算,因此必须相等。刚才已替换。我将查看代码审查以备将来使用,谢谢您的建议!这不是为了“简化”,而是从头开始写。什么是负价格?比较double是邪恶的,我投票结束这个问题,因为它要求我们检查工作代码。我想通过在整个代码中添加不同的错误检查过于复杂了。在开始时使用continue
语句添加条件要简单得多。这正是我希望了解的,我真的很感谢你抽出时间!谢谢!
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!");
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;
}
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!");