Pedro.A
Pedro.A

Reputation: 3

while-loop isn't working with two if-statements

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.

Upvotes: 0

Views: 618

Answers (3)

Evan Bechtol
Evan Bechtol

Reputation: 2865

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.

Upvotes: 1

Md. Samsuzzoha
Md. Samsuzzoha

Reputation: 76

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

    }
}
}

Upvotes: 0

Elentriel
Elentriel

Reputation: 1237

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

Upvotes: 0

Related Questions