gpt4 book ai didi

java - 我在访问 if else 语句中初始化的总价时遇到问题

转载 作者:行者123 更新时间:2023-12-01 11:55:04 25 4
gpt4 key购买 nike

如何获得红丝绒、奶油和 Vanilla 的 if else 语句中三个总价格的总和?并显示在allTextArea文本区域中?这是我的代码:

    double redVelvetPrice = 4.00;
double buttercreamPrice = 7.00;
double vanillaPrice = 3.00;
double firstHalf = 6.00;
double firstDozen = 12.00;
double firstBox = 25;
double secondHalf = 6.00;
double secondDozen = 12.00;
double secondBox = 25;
double thirdHalf = 6.00;
double thirdDozen = 12.00;
double thirdBox = 25;

String s1 = "";
String s2 = "The flavors you selected is/are: \n";
String s3 = "";
String s4 = "";
String s5 = "";
String totalPrice = "";


if (redVelvetCB.isSelected()){
s1=s1+ " "+ redVelvetCB.getText() + '\n';

if (firstHalfRB.isSelected()){
s3 ="A " + firstHalfRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstHalf + '\n' + '\n';

}
else if (firstDozenRB.isSelected()){
s3 =firstDozenRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstDozen + '\n' + '\n';

}
else if (firstBoxRB.isSelected()){
s3 =firstBoxRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstBox + '\n' + '\n';

}

}


if (buttercreamCB.isSelected()){
s1=s1+ " "+ buttercreamCB.getText() + '\n';

if (secondHalfRB.isSelected()){
s4 ="A " + secondHalfRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondHalf + '\n' + '\n';

}
else if (secondDozenRB.isSelected()){
s4 =secondDozenRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondDozen + '\n' + '\n';

}
else if (secondBoxRB.isSelected()){
s4 =secondBoxRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondBox + '\n' + '\n';

}


}

if (vanillaCB.isSelected()){
s1=s1+ " "+ vanillaCB.getText() + '\n';

if (thirdHalfRB.isSelected()){
s5 ="A " + thirdHalfRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdHalf + '\n' + '\n';

}
else if (thirdDozenRB.isSelected()){
s5 =thirdDozenRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdDozen + '\n' + '\n';

}
else if (thirdBoxRB.isSelected()){
s5 =thirdBoxRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdBox + '\n' + '\n';

}

allTextArea.setText(greeting+s2+"\n"+s1+"\n"+s3+s4+s5+"\nTotal Price is "+totalPrice+".\n"+deliv+payment+bye);

我尝试将总价格设置为 redVelvetPrice + butercreamPrice + vanillaPrice,但答案是 14.0,这是我在 if else 语句之外初始化的变量的总和 (4.00,7.00,3.00),我希望它是选择每种口味的数量后三者的价格之和。帮助我。

最佳答案

您应该为 totalPrice 使用其他数据类型。使用数字比使用字符串更容易计算。

double totalPrice = 0f;

之后,将 if/else 语句中的所有不同组合相加

if (firstHalfRB.isSelected()){
s3 = "A " + firstHalfRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstHalf + "\n\n";
totalPrice += (redVelvetPrice * firstHalf);
}
else if (firstDozenRB.isSelected()){
s3 = firstDozenRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstDozen + "\n\n";
totalPrice += (redVelvetPrice * firstDozen);
}

在 TextArea 中打印总计时,您可能需要对其进行格式化,使其在小数点后只有 2 位:

String.format("%.2f", totalPrice);

关于java - 我在访问 if else 语句中初始化的总价时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28522066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com