Skip to content
Advertisement

Can’t implement string output

        System.out.println("Write how many ml of water the coffee machine has: ");
    int waterInMachine = scanner.nextInt();
    System.out.println("Write how many ml of milk the coffee machine has: ");
    int milkInMachine = scanner.nextInt();
    System.out.println("Write how many grams of coffee beans the coffee machine has: ");
    int beansInMachine = scanner.nextInt();
    System.out.println("Write how many cups of coffee you will need: ");
    int countCups = scanner.nextInt();

    int water = 200 * countCups;
    int milk = 50 * countCups;
    int coffeeBeans = 15 * countCups;

    int amountWater = waterInMachine;
    int amountMilk = milkInMachine;
    int amountCoffeeBeans = beansInMachine;

    int count = 0;

    while (amountWater > 200 && amountMilk > 50 && amountCoffeeBeans > 15) {
        amountWater -= 200;
        amountMilk -= 50;
        amountCoffeeBeans -= 15;
        count++;
    }

    if (waterInMachine >= water && milkInMachine >= milk && beansInMachine >= coffeeBeans && count > countCups) {
        System.out.println("Yes, I can make that amount of coffee (and even " + (count - countCups) + " more than that)");
    } else if (waterInMachine >= water && milkInMachine >= milk && beansInMachine >= coffeeBeans) {
        System.out.println("Yes, I can make that amount of coffee");
    } else if (count < countCups) {
        System.out.println("No, I can make only " + count + " cup(s) of coffee");
    }

It is not possible to display the string correctly by condition

If the coffee machine has enough supplies to make the specified amount of coffee, the program should print “Yes, I can make that amount of coffee”. If the coffee machine can make more than that, the program should output “Yes, I can make that amount of coffee (and even N more than that)”, where N is the number of additional cups of coffee that the coffee machine can make. If the amount of resources is not enough to make the specified amount of coffee, the program should output “No, I can make only N cup(s) of coffee”.

Like in the previous stage, the coffee machine needs 200 ml of water, 50 ml of milk, and 15 g of coffee beans to make one cup of coffee.

When outputting by condition, everything is correct, except for the line

 if (waterInMachine> = water && milkInMachine> = milk && beansInMachine> = coffeeBeans && count> countCups) {
            System.out.println ("Yes, I can make that amount of coffee (and even" + (count) + "more than that)"); 
}

If I enter 600 153 100 1, then it will be Yes, I can make that amount of coffee (and even 1 more than that) but this right Yes, I can make that amount of coffee (and even 2 more than that)

Advertisement

Answer

You are making coffees while you have more than enough ingredients.
That means you won’t make a coffee when you have exactly the right amount of ingredients.

Try changing this:

while (amountWater > 200 && amountMilk > 50 && amountCoffeeBeans > 15)

To this:

while (amountWater >= 200 && amountMilk >= 50 && amountCoffeeBeans >= 15)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement