【问题标题】:Pizza price calculating using 'if' statements in java [duplicate]在java中使用'if'语句计算披萨价格[重复]
【发布时间】:2019-11-23 19:30:39
【问题描述】:

我试图让 Java 代码与用户交流。

代码是关于计算披萨价格的,假设披萨价格是最终的 int 并且不会改变。 唯一影响价格的是顾客想要在比萨上添加的东西(番茄、蘑菇、切达干酪)。 我尝试创建涵盖客户使用“if”语句选择的每个选项的代码, 但我认为有更简单的方法可以做到这一点。 (我希望程序仅根据附加名称计算价格。)

例如,客户选择了蘑菇和番茄,因此披萨价格将为披萨价格 + 番茄价格 + 蘑菇价格。

有没有更简单的解决方法? 还是应该用 if/else 语句涵盖客户选择的每个选项?

public static void main(String[] args){
    Scanner s= new Scanner(System.in);
    final int pizzaPrice= 12;
    int Mushrooms = 2;
    int Tomato= 2;
    int Corn = 2;
    int Olives = 2;
    int CheddarCheese=3;
    int bulgaritCheese=3;
    int yellowCheese= 3;

    String cheapAddOns ="Mushrooms , Tomato, Corn, Olives";
    String expensiveAddOns = "Cheddar cheese , Bulgarit cheese , Yellow cheese";

    System.out.println("Please enter first AddOns(Mushrooms , Tomato , Corn, Olives): ");
    cheapAddOns=s.next();
    System.out.println("Please enter second AddOns (Cheddar cheese , Bulgarit cheese ,Yellow cheese)");
    expensiveAddOns=s.next();

    if( cheapAddOns== "Mushrooms" || expensiveAddOns=="Cheddar cheese"  ) {
        System.out.println("Your Pizza price is: $" + pizzaPrice + Mushrooms + CheddarCheese );
    }
    else if (cheapAddOns=="Tomato" || expensiveAddOns=="Bulgarit cheese") {
        System.out.println("Your Pizza price is: $" +pizzaPrice+ Tomato + bulgaritCheese);
    }
}

【问题讨论】:

  • ==比较字符串是行不通的。
  • 您可以考虑将您的附加组件及其价格放在地图中。这将简化您的代码。您也可以计算第一个附加组件的价格,然后计算第二个附加组件的价格,然后将它们相加,而不是成对处理。
  • 您正在使用“||”什么时候应该使用“&&”。

标签: java string if-statement


【解决方案1】:

首先,当您命名变量时,不要以大写字母开头 int mushrooms,而不是 int Mushrooms。 第二件事,当您比较字符串时,== 运算符将不起作用。你必须使用stringName.equals()。在您的情况下,它看起来像:

cheapAddOns = s.next();
if(cheapAddOns.equals("tomato") || cheapAddOns.equals("mushrooms") || ...){
   //this way you can get one if for all cheap addons;
   pizzaPrice += 2; //read below to undrstand why i would add price this way
}

同样检查昂贵的插件。
当您启动cheapAddOnsexpensiveAddOns 时所做的操作不正确,我的意思是您使用 start 变量启动它们,然后从标准输入中读取它们。启动它们没有任何价值:

String cheapAddOns;
String expensiveAddOns;

对于这个例子,你不必使用 final int,最好将它作为一个普通整数初始化,如果任何语句为真,添加到这个值。它看起来像这样:

int pizzaPrice = 12;
int cheapAddonPrice = 2;
int expensiveAddonPrice = 3;
if(anyStatement){
   pizzaPrice += 2; //2 for cheap, 3 for expensiv addon
}
System.out.println("Your pizza costs: $" + pizzaPrice) 

只有当所有便宜的插件都花费 2 美元并且所有昂贵的插件都花费 3 美元时,它才有效。如果每个插件都有另一个价格,您将需要更多 if 语句来计算价格。 但是在尽可能多的语句中计算价格并打印一次(直到您只打印价格(没有插件列表)。
你在这里犯了很多简单的错误,所以我认为你才刚刚开始你的编程冒险。不要学习坏习惯,试着每天改进你的代码。

【讨论】:

    【解决方案2】:

    如果所有便宜的附加组件都具有相同的价格,一种方法可能是:

    String cheapAddOns = "Mushrooms, Tomato, ...";
    int cheapPrices = 2;
    
    System.out.println("Please enter first AddOns(Mushrooms , Tomato , Corn, Olives): ");
    String cheap = s.next();
    int price = pizzaPrice;
    if ( cheapAddOns.indexOf(cheap) >= 0 ) {
       price += cheapPrice;
    }
    

    然后使用昂贵的附加组件重复代码。 请注意,在调用 s.next() 以获取用户的下一个输入之前,您需要使用 CR。

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 2021-02-11
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多