- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
总而言之,此代码应该询问客户姓名、成员(member)级别和原始购买价格。它应该将所有这些存储在各自的变量中,如果输入了除接受的成员(member)级别之外的其他内容,则程序应该退出。所有变量存储完毕后,应按照本文末尾的格式打印出客户姓名、成员(member)级别、原购买价格、促销价格和节省金额。另外,您能否告诉我我的变量命名是否正确以及整个代码的格式是否正确。预先感谢您的帮助!
import java.util.*;
public class Discount{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
double purchase = 0.0;
double platinum = (purchase * .2);
double gold = (purchase * .15);
double silver = (purchase * .1);
String Platinum = null;
String Gold = null;
String Silver = null;
String customerName = null;
String level = null;
double discount = 0.0;
System.out.print("Please enter a customer name: ");
customerName = keyboard.nextLine();
System.out.print("Please enter the customer's member level: ");
level = keyboard.nextLine();
如果打印了 Silver、Gold 或 Platinum 以外的任何内容,则该部分应该退出程序;但是,无论输入什么内容,它都会退出。如果我省略这部分,它会继续询问原始购买价格是多少,但不会打印任何结果。
if (level != "Platinum" || level != "Gold" || level != "Silver")
{
System.exit(0);
}
System.out.print("Please enter the origianl purchase price: ");
purchase = keyboard.nextDouble();
if (level.equals(Platinum) && purchase > 500)
{
discount = (platinum - (purchase*.05));
System.out.println("Congratualations, " + customerName + "!");
System.out.println("As a " + level + " level cardholder, you have received a 25% discount during Bedlam.");
System.out.printf("%.2f\n", "Origianl purchase price: $" + purchase);
System.out.printf("%.2f\n", "Promotional price: $" + discount);
System.out.printf("%.2f\n", "Amount saved: $" + (purchase-discount));
}
else if (level.equals(Platinum))
{
discount = (platinum);
System.out.println("Congratualations, " + customerName + "!");
System.out.println("As a " + level + " level cardholder, you have received a 20% discount during Bedlam.");
System.out.printf("%.2f\n", "Origianl purchase price: $" + purchase);
System.out.printf("%.2f\n", "Promotional price: $" + discount);
System.out.printf("%.2f\n", "Amount saved: $" + (purchase-discount));
}
else if (level.equals(Gold))
{
discount = (gold);
System.out.println("Congratualations, " + customerName + "!");
System.out.println("As a " + level + " level cardholder, you have received a 15% discount during Bedlam.");
System.out.printf("%.2f\n", "Origianl purchase price: $" + purchase);
System.out.printf("%.2f\n", "Promotional price: $" + discount);
System.out.printf("%.2f\n", "Amount saved: $" + (purchase-discount));
}
else if (level.equals(Silver))
{
discount = (silver);
System.out.println("Congratualations, " + customerName + "!");
System.out.println("As a " + level + " level cardholder, you have received a 10% discount during Bedlam.");
System.out.printf("%.2f\n", "Origianl purchase price: $" + purchase);
System.out.printf("%.2f\n", "Promotional price: $" + discount);
System.out.printf("%.2f\n", "Amount saved: $" + (purchase-discount));
}
/* Example:
Congratulations, Marge Simpson!
As a Silver level cardholder, you received a 10% discount during Bedlam.
Original purchase price: $500.20
Promotional price: $450.18
Amount saved: $50.02 */
}
}
最佳答案
当通过错误的客户级别时,您的程序不会退出,因为比较检查不正确。您需要使用 equals
方法进行字符串比较:
if (level != "Platinum" || level != "Gold" || level != "Silver")
更改为
if (!level.equals("Platinum") || !level.equals("Gold") || !level.equals("Silver"))
!=
不会比较字符串内容,而是检查对象是否相等。
编辑从威利的回答中我意识到你需要使用 && 而不是 ||在你的 if 检查中。所以正确的条件应该是:
if (!level.equals("Platinum") && !level.equals("Gold") && !level.equals("Silver"))
关于Java代码,打印客户姓名、折扣级别、原始购买价格、促销价格和节省的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927750/
我有一个 rails/spree 商店应用程序,它使用 spree's "User" rule 进行长期促销.问题是,在促销事件期间,为商店中促销不适用的每个订单创建了不合格的调整。这在数据库中增加了
我想创建一个列来标记员工,只要他们得到晋升(等级变化),如下例所示: +-----------+------------+------------+-------+------+ | PERSON_I
我无法使用 App Store 促销 推广我的应用内购买,因为二进制文件不包含 SKPaymentTransactionObserver 方法,如下所示: These in-app purchases
我是一名优秀的程序员,十分优秀!