1

Edit: Issue was I didn't declare variables outside it

My loop cannot resolve an int as a variable.

Can't try other types of variables, this has to be an integer.

        do {
            System.out.println("Would you like to add any snacks? ");
            System.out.println("1: ($5) Large Popcorn");
            System.out.println("2: ($0) Nothing Else");
            int snackChoice = input.nextInt();

            System.out.println("How many of option " + snackChoice + " would you like? ");
            System.out.println("1: One");
            System.out.println("2: Two");
            System.out.println("3: Three");
            System.out.println("4: Four");
            System.out.println("5: Five");
            int snackAmount = input.nextInt();

            switch (snackChoice) { 
            case 1: 
                cost = cost + 5 * snackAmount;
                System.out.println("Your total before tax will be: $" + cost + (5 * snackAmount)); 
            break;

            default: 
                System.out.println("Not a valid option."); 
            break;

            }
        }
        while(snackChoice != 9);

Rest of it: https://pastebin.com/Y6Xd24D0

It works. Actual result: It doesn't.

Errors: snackChoice cannot be resolved to a variable

1 Answer 1

3

the do...while loop is a code block, which means that variables defined in it cannot be seen outside the block. The while part is outside the block, so it cannot see snackChoice.

Just define int snackChoice before the do...while, not in it:

int snackChoice;
do {
    System.out.println("Would you like to add any snacks? ");
    System.out.println("1: ($5) Large Popcorn");
    System.out.println("2: ($0) Nothing Else");
    snackChoice = input.nextInt();

    System.out.println("How many of option " + snackChoice + " would you like? ");
    System.out.println("1: One");
    System.out.println("2: Two");
    System.out.println("3: Three");
    System.out.println("4: Four");
    System.out.println("5: Five");
    int snackAmount = input.nextInt();

    switch (snackChoice) { 
    case 1: 
        cost = cost + 5 * snackAmount;
        System.out.println("Your total before tax will be: $" + cost + (5 * snackAmount)); 
    break;

    default: 
        System.out.println("Not a valid option."); 
    break;

    }
}
while(snackChoice != 9);
Sign up to request clarification or add additional context in comments.

4 Comments

I have snackChoice 9 to exit the loop, is there another way I can exit the loop with a choice? I need the user to be able to choose as many snacks and quantities as they want.
That's fine. Just define the variable itself before the do { statement - so it'll exist ouside the do..while block
@BellaStallman There, I've added the modified code to my answer.
Alright thank you. When running, and I try option 9 now, it asks me how many I'd like. No matter which option I choose (1, 5, 12) it gives me invalid option and asks again. How can I exit this properly now?

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.