0

So I have been trying to figure this out for the past 5 hours and the reason being...from a beginner point of view this code looks fine with no errors but the output is still wrong...basically for every 10 kids tickets the user gets one free,so the output is fine if the free tickets is less than the adults but it subtracts the extra adults when its the other way around.

package theatre;

import java.util.*;

public class Theatre {

    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        System.out.println("Enter the number of tickets you would like to buy for each type: ");
        System.out.println("Adult:          £10.50");
        System.out.println("Child:          £7.30");
        System.out.println("Concessions     £8.40");
        System.out.println("Adult: ");
        int Adult = kybd.nextInt();
        System.out.println("Child: ");
        int Child = kybd.nextInt();
        System.out.println("Concessions: ");
        int concessions = kybd.nextInt();

        int freeAdult = Child / 10;
        if (freeAdult < 9) {
            System.out.println("You get " + freeAdult + " adult tickets");
        } else {
            System.out.println("You don't get any free tickets");
        }
        System.out.println(freeAdult); //reference out
        double adultBill = (Adult * 10.50) - (freeAdult * 10.50);
        double childBill = Child * 7.30;
        double concessionsBill = concessions * 8.40;
        double totalBill = adultBill + childBill + concessionsBill;
        double totalBill2 = childBill + concessionsBill;

        System.out.println(totalBill2); //reference out
        if (freeAdult >= Adult) {
            System.out.printf("Total bill is: £%.2f\n", totalBill);
        } else if (freeAdult < Adult) {

            System.out.printf("Total bill is: £%.2f\n", totalBill2);
        } else {
            System.out.println("Please enter a corrrect value");
        }



    }

}

Thanks

8
  • 5
    use double to define freeAdult instead of int Commented Oct 21, 2015 at 16:54
  • that did not work,thanks though Commented Oct 21, 2015 at 16:57
  • Why do you create a totalBill2? That doesn't contain any adult tickets, that's why it subtracks the extra adult tickets when there are more free adult ticket than non-free. Commented Oct 21, 2015 at 17:00
  • And you also should examine freeAdult < Adult statement before this: double adultBill=(Adult*10.50)-(freeAdult*10.50); because this way a negative bill also can be generated like the cashier would pay to the visitor. Commented Oct 21, 2015 at 17:02
  • 2
    @Adem -- using a double to calculate freeAdult does not help. The adultBill shouldn't care about "partial adults". Commented Oct 21, 2015 at 17:03

2 Answers 2

2

I think you have your logic a little confused:

int freeAdult = Child / 10;
if (freeAdult < 9) {
    System.out.println("You get " + freeAdult + " adult tickets");
} else {
    System.out.println("You don't get any free tickets");
}

appears to be calculating how many free adult tickets the person gets based on the number of child tickets (Child / 10) ...but is then saying they get no tickets if they've qualified for more than 10? I think you might be using the wrong variable here. Then:

double adultBill = (Adult * 10.50) - (freeAdult * 10.50);
double childBill = Child * 7.30;
double concessionsBill = concessions * 8.40;
double totalBill = adultBill + childBill + concessionsBill;
double totalBill2 = childBill + concessionsBill;
System.out.println(totalBill2); //reference out
if (freeAdult >= Adult) {
    System.out.printf("Total bill is: £%.2f\n", totalBill);
} else if (freeAdult < Adult) {
    System.out.printf("Total bill is: £%.2f\n", totalBill2);
} else {
    System.out.println("Please enter a corrrect value");
}

I believe is meant to calculate 2 bills, one including adults and one not and then if the number of free adult tickets is greater than the number of adults, discard the first bill and use the second (which never included the adults calculation), in which case you have your if statement the wrong way around, it should be:

if (freeAdult < Adult) {
    System.out.printf("Total bill is: £%.2f\n", totalBill);
} else if (freeAdult >= Adult) {
    System.out.printf("Total bill is: £%.2f\n", totalBill2);

...however you should only have to calculate one bill by simply changing the adult calculation to:

double adultBill = freeAdult < Adult ? (Adult * 10.50) - (freeAdult * 10.50) : 0;

Then calculate bill as normal:

double totalBill = adultBill + childBill + concessionsBill;
System.out.printf("Total bill is: £%.2f\n", totalBill);
Sign up to request clarification or add additional context in comments.

1 Comment

YES,it worked,thanks alot..everyone.I should have come down here earlier
0
public class Theatre {

    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        System.out.println("Enter the number of tickets you would like to buy for each type: ");
        System.out.println("Adult:          £10.50");
        System.out.println("Child:          £7.30");
        System.out.println("Concessions     £8.40");
        System.out.println("Adult: ");
        int adult = kybd.nextInt();
        System.out.println("Child: ");
        int child = kybd.nextInt();
        System.out.println("Concessions: ");
        int concessions = kybd.nextInt();

        int freeAdult = child / 10;
        System.out.println("You get " + freeAdult + " adult tickets free");
        if(adult >0){
            adult = adult - freeAdult;
        }
        double adultBill = (adult * 10.50);
        double childBill = child * 7.30;
        double concessionsBill = concessions * 8.40;
        double totalBill = adultBill + childBill + concessionsBill;

        System.out.printf("Total bill is: £%.2f\n", totalBill);

    }

}

1 Comment

just tried your code @yogidilip and it still subtracts the extra adult. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.