-1

I need to write a program that calculates beverages for an entered amount of money. It was working before but I don't know if NetBeans just got tired of doing stuff or what because it suddenly couldn't get past the inputs. I can't figure out what I need to change to get it to function properly again and I can only assume it's the while loop that it's getting stuck on.

I have tried changing numbers, deleting spaces, altering the while conditions, moving line breaks around, and nothing works. Here is the official question: Johnny is at the bar and he is going to drink beer. Write a program that computes how many beers he can buy for money that he has. The program reads the amount and the price of beer, and prints how many beers he can afford. Consider also tax (10%) and tips (20%). Print the result in the following form: If a beer costs $3.25, Johnny can have 3 beers for $15 (he will pay $12.87).

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double br;
    double amt;
    double taxPrc;
    double bill;
    int count = 0;
    System.out.printf("enter ur name: ");
    String name = sc.nextLine();
    System.out.printf("Enter price of beverage: $");
    br = sc.nextDouble();
    System.out.printf("Enter amt %s has: $", name);
    amt = sc.nextDouble();
    taxPrc = br * 1.1;
    bill = taxPrc * 1.2;

    while(bill<amt) {
        count++;
        bill = taxPrc * 1.2;
    }
    System.out.printf("if bevergae costs $"+br+", "+name+" can have "+count+" beverges for $"+amt+" (the bill will be $"+bill+").");
    System.out.println();
}

My editor isn't showing that there are any problems. The file runs: "enter ur name (name), Enter price of beverage $(#), Enter amt (name) has $(input number)" then it just stops showing anything and leaves me on a blank until I stop it. It's supposed to go on "if beverage costs $X, [name] can have [#] beverages for $[#] (the bill will be $[#])." I was having an issues trying to get it to display the correct number for the bill less than the initial amount entered when it stopped working.

Katochalol
  • 21
  • 3
  • 1
    Have you checked what `bill` is? If that loop is going forever, `bill` must be always less than `amt`. – Carcigenicate Sep 09 '19 at 22:06
  • 2
    Before the while loop you have set bill = taxPrc * 1.2 and during the while loop you also set bill = taxPrc * 1.2. taxPrc never changes so you are always setting bill to the same amount, this means that bill will never be less than amt so the loop will run forever – Chachmu Sep 09 '19 at 22:08
  • 1
    apparently, when I added "System.out.printf("%s", bill); it would display everything correctly again – Katochalol Sep 09 '19 at 22:09
  • It all depends on the value which you entered as an input. If you enter a large value for amt and a very small value for br, the loop will run for a long time. Otherwise, there is no issue with the code now that it is running again – MRTJ Sep 09 '19 at 22:11
  • 2
    thank you @Chachmu I figured out that I accidentally deleted "bill +" in the while loop <3 – Katochalol Sep 09 '19 at 22:12
  • 3
    Next time, use a **debugger**, and you would have had your answer a lot faster than writing a question here. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 09 '19 at 22:13

1 Answers1

3

Just think about this block of code on its own for a bit

bill = taxPrc * 1.2;
while(bill<amt) {
        count++;
        bill = taxPrc * 1.2;
    }

? What in the while loop changes either bill or amt? Remember, a while loop runs until something in its conditional statement (in this case bill<amt) changes. As nothing in the while loop changes anything in the condition statement, it runs forever.

Your code doesn't change amt at all and just keeps resetting bill to the same value.

Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35