подскажите пжл, использую switch и в двух случаях из 5 происходит "магия". Переменная str типа string, вместо считывания из консоли принимает ""

    import java.util.Scanner;
    public class CoffeeMachine {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int water = 400;
    int milk = 540;
    int cofeeBeans = 120;
    int cups = 9;
    int money = 550;
    boolean flag = true;
    String str;


    while (flag){ //программа симулирует работу кофейного автомата
     System.out.println("Write action (buy, fill, take, remaining, exit):");
     str = in.nextLine();// при выборе "buy"->"1". почему-то str ="", но не считывает из консоли
            switch (str) {
             case "buy":{
      System.out.println(" What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - 
    to main menu:");
                int chouse = in.nextInt();
                 switch (chouse) {
                     case 1: { //espresso
                         if ((water < 250) ^ (cofeeBeans < 16) ^ (cups < 1))
                             System.out.println("I have enough resources, making you a coffee!");
                         else {
                             water = water - 250;
                             cofeeBeans = cofeeBeans - 16;
                             cups = cups - 1;
                             money = money + 4;
                             print1(water, milk, cofeeBeans, cups, money);
                         }
                     }
                     break;

                     case 2: {//latte
                         if ((water < 350) ^ (cofeeBeans < 20) ^ (cups < 1) ^ (milk < 75))
                             System.out.println("I have enough resources, making you a coffee!");
                         else {
                             water = water - 350;
                             cofeeBeans = cofeeBeans - 30;
                             cups = cups - 1;
                             money = money + 7;
                             milk = milk - 75;
                             print1(water, milk, cofeeBeans, cups, money);
                         }
                     }
                     break;

                     case 3: {//cappuccino
                         if ((water < 200) ^ (cofeeBeans < 12) ^ (cups < 1))
                             System.out.println("I have enough resources, making you a coffee!");
                         else {
                             water = water - 250;
                             cofeeBeans = cofeeBeans - 12;
                             cups = cups - 1;
                             money = money + 6;
                             print1(water, milk, cofeeBeans, cups, money);
                         }
                     }
                     break;
                }
                }break;
             case "fill":
                System.out.println("Write how many ml of water do you want to add: ");
                 int water2 = in.nextInt(); water = water + water2;
                           System.out.println("Write how many ml of milk do you want to add:  ");
                  int milk2 = in.nextInt(); milk = milk + milk2;
                           System.out.println("Write how many grams of coffee beans do you want to 
  add: ");
                  int cofee2 = in.nextInt(); cofeeBeans = cofeeBeans + cofee2;
                            System.out.println("Write how many disposable cups of coffee do you want 
  to add: ");
                  int cups2 = in.nextInt(); cups = cups + cups2;
                 print1(water, milk, cofeeBeans, cups, money);

             break;

             case "take":
                System.out.println("I gave you $" + money);
                   money = money - money;
                print1(water, milk, cofeeBeans, cups, money);
             break;

             case "remaining":
                print1(water, milk, cofeeBeans, cups, money);
             break;

             case "exit":
                    flag=false;
             break;

             default:
                    System.out.println("какого хрена ты выполняешься");//добавил чтобы смотрет
        }
    }
}
private static void print1(int water, int milk, int cofeeBeans, int cups, int money) {
    System.out.println("The coffee machine has:");
    System.out.printf("%d of water",water);System.out.println();
    System.out.printf("%d of milk",milk);System.out.println();
    System.out.printf("%d of coffee beans",cofeeBeans);System.out.println();
    System.out.printf("%d disposable cups",cups);System.out.println();
    System.out.printf("%d of money",money);System.out.println();
}


}

Ответы (1 шт):

Автор решения: Igor

Потому что nextInt не считывает конец строки.

int chouse = in.nextInt();
in.nextLine();
→ Ссылка