简体   繁体   中英

Write a program that will ask the user for the number of tickets and output total

Write a program that will ask the user for the number of adult tickets, the number of children's tickets, whether they want reserve or general admission, and whether they have a radio voucher to use. Calculate the cost of the order.

There are two levels of tickets, Reserved for $55.00 each or General Admissions at $35.00 each. Kids under 12 are half off. The local radio station is running a special. If you call in, they will send you a voucher that will give you a 20% discount.

All orders over $200 get a 10% discount on the final price (after other discounts are applied) and those over $400 get 15% off.

My code so far...

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

}

Asks all of the questions correctly but doesn't return an output. This is a simple Java program so I would like the simplest answer possible

The problem is that none of your if s and else s are firing correctly because you're comparing your strings incorrectly.

Instead of if (str == "value") you need if (str.equals("value")) everywhere. In cases where you want a case-insensitive match like if (str == "value" || str == "VALUE") you need if (str.equalsIgnoreCase("value")) instead.

Please check out How do I compare Strings in Java? for more info

Couple of issues:

  • When testing for equality amongst objects, you should always use equals method and now "==" which means you are comparing two object reference. So change the following from

    if (tixType == "GA" || tixType == "ga")

With

if (tixType.equalsIgnoreCase("ga")) 

Same for other string comparison.

  • After asking for whether user has radio voucher or not, you should then only do calculation related to radio voucher like:

     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 ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM