编写一个程序,询问用户门票数量和输出总数

时间:2015-02-07 06:29:32

标签: java boolean output println

编写一个程序,询问用户成人票的数量,儿童票的数量,他们是否需要预订或一般入场,以及他们是否有使用的无线电凭证。计算订单的成本。

有两种级别的门票,每张55美元或一般入场费为35美元。 12岁以下的儿童可享半价优惠。     当地广播电台正在运行一个特殊的电台。如果您打电话,他们会向您发送一张优惠券,可以给您20%的折扣。

所有超过200美元的订单在最终价格上获得10%的折扣(在应用其他折扣后),超过400美元的订单可获得15%的折扣。

我的代码到目前为止......

public static void main(String[] args) {
    // variables
    int adultTix;
    int childTix;
    int GENERAL_ADMISSION = 35;
    int RESERVED = 55;
    double radioDiscount = .20;
    double ticketTotal = 0;

    Scanner scan = new Scanner(System.in);
    System.out.println("How many adult tickets?");
    adultTix = scan.nextInt();
    System.out.println("How many kids tickets?");
    childTix = scan.nextInt();
    scan.nextLine();
    System.out.println("Reserved tickets are $55 each and General Admission is $35."
                    + " Would you like Reserved or General Admission? (enter GA or RE only):");
    String tixType = scan.nextLine();
    if (tixType == "GA" || tixType == "ga") 

        ticketTotal = ((adultTix * GENERAL_ADMISSION) + ((childTix * GENERAL_ADMISSION) / 2));
    else if (tixType == "RE" || tixType == "re")
        ticketTotal = ((adultTix * RESERVED) + ((childTix * RESERVED) / 2));

    System.out.println("Do you have a radio voucher? (enter yes or no):");
    String radioQ = scan.nextLine();

    if (radioQ == "yes")
        System.out.print("With the radio discount, you will save 20%!");
         if (radioQ == "no")
            System.out.println("No radio discount.");

         double radioT;
    radioT = ((ticketTotal - (ticketTotal * radioDiscount)));
    if (radioT >= 200 && radioT < 400)
        System.out.println("With a 10% discount, your total is: $"
                + (radioT * .9));
    else if (radioT > 400)
        System.out.println("With a 15% discount, your total is: $"
                + (radioT * .85));
    scan.close();
}

}

正确地询问所有问题,但没有返回输出。这是一个简单的Java程序,所以我希望得到最简单的答案

2 个答案:

答案 0 :(得分:0)

问题是,由于您未正确比较字符串,因此ifelse都没有正确触发。

而不是if (str == "value"),而是在任何地方都需要if (str.equals("value"))。如果您需要不区分大小写的匹配项if (str == "value" || str == "VALUE"),则需要if (str.equalsIgnoreCase("value"))

请查看How do I compare Strings in Java?以获取更多信息

答案 1 :(得分:0)

几个问题:

  • 在测试对象之间的相等性时,应始终使用equals方法,现在使用“==”,这意味着您要比较两个对象引用。因此,请从

    更改以下内容

    if(tixType ==“GA”|| tixType ==“ga”)

使用

if (tixType.equalsIgnoreCase("ga")) 

其他字符串比较相同。

  • 在询问用户是否有无线电凭证后,您应该只进行与无线电凭证相关的计算,如:

    if (radioQ == "yes")//use equalsIgnoreCase method of String
       System.out.print("With the radio discount, you will save 20%!");
       radioT = ((ticketTotal - (ticketTotal * radioDiscount)));
       if (radioT >= 200 && radioT < 400)
           System.out.println("With a 10% discount, your total is: $"
            + (radioT * .9));
       else if (radioT > 400)
           System.out.println("With a 15% discount, your total is: $"
             + (radioT * .85));
       //apply discount on ticket total?
    } else ...
    
相关问题