打印客户名称,折扣级别,原始购买价格,促销价格和保存金额的Java代码

时间:2013-09-21 00:39:19

标签: java if-statement

总而言之,此代码应该询问客户姓名,会员级别和原始购买价格。它应该将所有这些存储在它们各自的变量中,如果输入了除接受的成员级别之外的其他内容,则程序应该退出。存储完所有变量后,客户的名称,会员级别,原始购买价格,促销价格和保存金额应以本文末尾的格式打印出来。另外,你能告诉我我的变量是否正确命名,以及整个代码是否格式正确。在此先感谢您的帮助!

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();

如果打印出银,金或白金以外的任何内容,则该部分应退出该程序;但是,无论输入什么内容,它都会退出。如果我将此部分退出,它将继续询问原始购买价格,但不会打印任何结果。

          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 */
}
}

2 个答案:

答案 0 :(得分:2)

由于比较检查不正确,当您传递错误的客户级别时,您的程序不会退出。您需要在此处使用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"))

答案 1 :(得分:0)

因为按照我想要的方式在另一条评论中解释我的评论几乎是不可能的,所以这是你评论的answer

您的部分代码是:

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));
    }

在此代码中,您为每个案例写出System.out.println("Congratualations, " + customerName + "!");一次,并且在每种情况下它们都是相同的。我的建议是写下你的代码:

if (level != "Platinum" || level != "Gold" || level != "Silver")
{
    System.exit(0);
}

System.out.print("Please enter the origianl purchase price: ");
purchase = keyboard.nextDouble();

System.out.println("Congratualations, " + customerName + "!");

if (level.equals(Platinum) && purchase > 500)
{
    discount = (platinum - (purchase*.05));
    System.out.println("As a " + level + " level cardholder, you have received a 25% discount during Bedlam.");
}
else if (level.equals(Platinum))
{
    discount = (platinum);
    System.out.println("As a " + level + " level cardholder, you have received a 20% discount during Bedlam.");
}
else if (level.equals(Gold))
{
    discount = (gold);
    System.out.println("As a " + level + " level cardholder, you have received a 15% discount during Bedlam.");
}
else if (level.equals(Silver))
{
    discount = (silver);
    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));