0

I have tried searching online everywhere and I cant. Basically I wanted to make a program where you have choices of what drinks you want, then when you type the drink, it tells how much it is. I used a while loop so if you enter the wrong price it keeps asking for the right price till you select it. It works fine for my first drink which is "pepsi" but then for when I pick coke, the code just doesnt run at all.. it doesnt even ask for the price.

import java.util.Scanner;
public class BendingMachineTestWhile{



 public static void main (String [] args)
  {
    String drink;
    int money;
    Scanner kbd = new Scanner (System.in);

System.out.println (" Pick from the following drinks: \n pepsi \n coke \n gatorade");
drink = kbd.nextLine();
while (true){     
  if (drink.equals ("pepsi"))
  {
    System.out.println ("That will be 4 dollars");
    System.out.println ("Enter 4 dollars");
    money = kbd.nextInt();
    if ( money == 4 )
    {
      System.out.println ("Thanks.");
      break;
    }
    else if ( money != 4)
    {
      System.out.println ("You idiot its four dollars");
    }

  }
}

if (drink.equals ("coke"))
{
  System.out.println ("That will be 6 dollars");
  System.out.println ("Enter 6 dollars");
  money = kbd.nextInt();
  while (true){
    if ( money == 6 )
    {
      System.out.println ("Thanks.");
      break;
    }
    else if ( money != 6)
    {
      System.out.println ("You idiot its four dollars");
    }
  }

}
}
}

The second if statement is the one that doesn't work. If you guys could help me it would be really appreciated.

1
  • 2
    Is because your if statment of coke is out of the loop. move inside the while loop Commented Nov 9, 2015 at 18:56

3 Answers 3

1

The problem is that your code for the coke selection is outside of your while loop. Thus, it never executes or even checks for coke inside the loop execution.

I formatted your code a bit to be more easily read. :)

By the way, your code for the "coke" condition is different than your pepsi condition. I would change them to be basically the same, since they are supposed to execute the same basic operation.

Change your loop to this:

while (true){     
    if (drink.equals ("pepsi"))
    {
        System.out.println ("That will be 4 dollars");
        System.out.println ("Enter 4 dollars");
        money = kbd.nextInt();
        if ( money == 4 )
        {
            System.out.println ("Thanks.");
            break;
        }
        else
        {
            System.out.println ("You idiot it's four dollars");
        }
    }
    else if (drink.equals ("coke"))
    {
        System.out.println ("That will be 6 dollars");
        System.out.println ("Enter 6 dollars");
        money = kbd.nextInt();
        if ( money == 6 )
        {
            System.out.println ("Thanks.");
            break;
        }
        else
        {
            System.out.println ("You idiot it's four dollars");
        }

    }
    else {
        System.out.println("You didn't make a valid choice, please choose coke or pepsi");
    }

    // Get another input to continue program, here.
}

You should really consider naming your variables something descriptive of their purpose. For example, your Scanner should be named something like: input. This makes it very clear what it does and then implies on how to use it based on the object that it is.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, I will change my variable names for my scanner and stuff too. thank you
@Pedro.A Very welcome! Best of luck on your programming journey
0

Try putting it inside you while(true) statement.

Also as more realistic scenario you could make an array with prices and names, but thats another issue entirely

Comments

0

I think it will good for you.

import java.util.Scanner;

public class BendingMachineTestWhile {

public static void main(String[] args) {
    String drink;
    int money;
    Scanner kbd = new Scanner(System.in);

    System.out.println(" Pick from the following drinks: \n pepsi \n coke \n gatorade");
    drink = kbd.nextLine();
    while (true) {
        if (drink.equals("pepsi")) {
            System.out.println("That will be 4 dollars");
            System.out.println("Enter 4 dollars");
            money = kbd.nextInt();
            if (money == 4) {
                System.out.println("Thanks.");
                break;
            } else if (money != 4) {
                System.out.println("You idiot its four dollars");
            }

        } else if (drink.equals("coke")) {
            System.out.println("That will be 6 dollars");
            System.out.println("Enter 6 dollars");
            money = kbd.nextInt();
            if (money == 6) {
                System.out.println("Thanks.");
                break;
            } else if (money != 6) {
                System.out.println("You idiot its four dollars");
            }
        } else {
            if (drink.equals("gatorade")) {
                System.out.println("That will be 8 dollars");
                System.out.println("Enter 8 dollars");
                money = kbd.nextInt();
                if (money == 8) {
                    System.out.println("Thanks.");
                    break;
                } else if (money != 8) {
                    System.out.println("You idiot its four dollars");
                }
            } else {
                System.out.println("Your entered drink is not available here so please enter from the list again");
                System.out.println(drink = kbd.nextLine());
            }
        }

    }
}
}

Comments

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.