如何将 2 个方法循环在一起?

How can I loop 2 methods together?

Question

我是 Java 的新手,我正在努力学习 can.The 我要解决的实际问题在上图中。如果我对问题的描述不够详细,请务必参考图片。我目前正在编写一个与买咖啡有关的 java 程序。我有一种方法可以计算购买咖啡的成本 (BuyCoffee) 和使用的资源量。也有不同类型的咖啡,每种咖啡使用不同数量的资源。我还有另一种方法可以计算咖啡售出后剩余的资源**(getRemainingResources)**。我在循环 getRemainingResources 方法时遇到问题。因此,当我购买一次咖啡时,我拥有的资源量就会减少。我有一种方法来填充这些资源。但每次我尝试再次购买相同的咖啡时,我所剩的资源量并没有减少。我的代码如下

import java.util.Scanner;

public class MethodsTest {
    //global variable--starting quantity of resources in machine
    static final int water = 400;
    static final int milk = 540;
    static final int coffeeBean = 120; // in g
    static int disposableCups = 9;
    static final int cost = 550; //in $

//global variable --refill quantity of resources in machine
static int fillWater, fillMilk, fillCoffeeBeans, fillDisposableCups;

//global variable -- remaining quantity of resources in machine
static int newRemCost, remCost, remWater, remMilk, remCoffeeBean, remDisposableCups;

static int espressoWater, espressoMilk, espressoCoffeeBeans, espressoCost; // Resources for espresso
static int latteWater, latteMilk, latteCoffeeBeans, latteCost;
static int cappuccinoWater, cappuccinoMilk, cappuccinoCoffeeBeans, cappuccinoCost;
static int regWater, regMilk, regCoffeeBeans, regCost;                         //Resources for regular coffees

static void FillCoffeeMachine() {
    Scanner input = new Scanner(System.in);
    System.out.println("Write how many ml of water you want to add: ");
    fillWater = input.nextInt();
    System.out.println("Write how many ml of milk you want to add: ");
    fillMilk = input.nextInt();
    System.out.println("Write how many grams of coffee beans you want to add: ");
    fillCoffeeBeans = input.nextInt();
    System.out.println("Write how many disposable cups of coffee you want to add: ");
    fillDisposableCups = input.nextInt();

    MainMenu();
}

static void BuyCoffee() {                                  //method for buying coffee
    disposableCups--;// always decreasing when coffee is sold
    class TypesOfCoffee {
        void Espresso() {                             //nested class, since there are different kind of coffee
            espressoWater = 250;
            espressoMilk = 0;
            espressoCoffeeBeans = 16;
            espressoCost = 4;


            if (remWater > espressoWater && remMilk > espressoMilk && remCoffeeBean > espressoCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < espressoWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remCoffeeBean < espressoCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void Latte() {
            latteWater = 350;
            latteMilk = 75;
            latteCoffeeBeans = 20;
            latteCost = 7;

            if (remWater > latteWater && remMilk > latteMilk && remCoffeeBean > latteCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < latteWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < latteMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < latteCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void Cappuccino() {
            cappuccinoWater = 200;
            cappuccinoMilk = 100;
            cappuccinoCoffeeBeans = 12;
            cappuccinoCost = 6;

            if (remWater > cappuccinoWater && remMilk > cappuccinoMilk && remCoffeeBean > cappuccinoCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < cappuccinoWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < cappuccinoMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < cappuccinoCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void regularCoffee() {
            regWater = 200;
            regMilk = 50;
            regCoffeeBeans = 15;
            regCost = 0;

            if (remWater > regWater && remMilk > regMilk && remCoffeeBean > regCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < regWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < regMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < regCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }
    }

    Scanner input = new Scanner(System.in);
    System.out.println("What kind of coffee would you like to buy today?");
    System.out.println("""
            Please enter:
            1 for espresso
            2 for latte
            3 for cappuccino
            4 for a regular coffee
            back - to go back to main menu""");
    String choice = input.next();

    switch (choice) { //lets user choose what type of coffee to buy
        case "1":
            new TypesOfCoffee().Espresso();
            break;
        case "2":
            new TypesOfCoffee().Latte();
            break;
        case "3":
            new TypesOfCoffee().Cappuccino();
            break;
        case "4":
            new TypesOfCoffee().regularCoffee();
            break;
        case "back":
            MainMenu();
            break;
        default:
            System.out.println("Please enter an option from the above");
    }
    MainMenu();
}


static void takeMoney() { //method to take the money
    System.out.println("I gave you" + " " + "$" + remCost);
    newRemCost = remCost;
    MainMenu();
}

static void getRemainingResources() {
    //TODO --has to loop..but it works only once
    remDisposableCups = fillDisposableCups + disposableCups;
    boolean found = remDisposableCups > 0; // true for coffee to be sold
    while (found) {
        remWater = water + fillWater - espressoWater - cappuccinoWater - latteWater;//
        remMilk = milk + fillMilk - espressoMilk - cappuccinoMilk - latteMilk;
        remCoffeeBean = coffeeBean + fillCoffeeBeans - espressoCoffeeBeans - cappuccinoCoffeeBeans - latteCoffeeBeans;
        remCost = cost + espressoCost + cappuccinoCost + latteCost - newRemCost;
        //found = remDisposableCups > 0 && remWater>=water && remMilk>=milk&&remCoffeeBean>=coffeeBean;

        System.out.println("The coffee machine has: ");
        System.out.println(remWater + " " + "ml of water");
        System.out.println(remMilk + " " + "ml of milk ");
        System.out.println(remCoffeeBean + " " + "g of coffee beans ");
        System.out.println(remDisposableCups + " " + "disposable cups ");
        System.out.println("$" + remCost + " " + "of money");
        found = remDisposableCups < 0;
    }

    MainMenu();
}

static void MainMenu() { // Gives user option to decide what they want to do
    Scanner input = new Scanner(System.in);
    System.out.println("Main Menu: " + "\n");
    System.out.println("Please choose from the following options below");
    System.out.println("""
            To buy coffee - Enter buy
            To fill the machine - Enter fill
            To get cash from machine - Enter take
            To see remaining resources - Enter remaining
            To exit program - Enter exit""");
    String choice = input.next();

    switch (choice) {
        case "buy":
            BuyCoffee();
            break;
        case "fill":
            FillCoffeeMachine();
            break;
        case "take":
            takeMoney();
            break;
        case "remaining":
            getRemainingResources();
            break;
        case "exit":
            System.exit(0);
            break;
        default:
            MainMenu();//goes back to main menu if user types in an unknown value
     }
}

public static void main(String[] args) {
    MainMenu();
}

}

现在应该走了 但是还有其他错误(除了扫描仪,他们已经告诉过你了) 例如,如果不先“剩余”,你就不能买咖啡(还有:静态变量太多,没有使用 OO ...)。这只是一个例子。 我更正了您要求的错误的代码,但还有很多地方需要更改。

import java.util.Scanner;

public class MethodsTest {
    //global variable--starting quantity of resources in machine
    static final int water = 40000;
    static final int milk = 54000;
    static final int coffeeBean = 12000; // in g
    static int disposableCups = 900;
    static final int cost = 550; //in $
    static int remCost = 550; //in $

//global variable --refill quantity of resources in machine
static int fillWater, fillMilk, fillCoffeeBeans, fillDisposableCups;

//global variable -- remaining quantity of resources in machine
static int newRemCost, remWater, remMilk, remCoffeeBean, remDisposableCups;

static int espressoWater, espressoMilk, espressoCoffeeBeans, espressoCost; // Resources for espresso
static int latteWater, latteMilk, latteCoffeeBeans, latteCost;
static int cappuccinoWater, cappuccinoMilk, cappuccinoCoffeeBeans, cappuccinoCost;
static int regWater, regMilk, regCoffeeBeans, regCost;                         //Resources for regular coffees

static void FillCoffeeMachine() {
    Scanner input = new Scanner(System.in);
    System.out.println("Write how many ml of water you want to add: ");
    fillWater = input.nextInt();
    System.out.println("Write how many ml of milk you want to add: ");
    fillMilk = input.nextInt();
    System.out.println("Write how many grams of coffee beans you want to add: ");
    fillCoffeeBeans = input.nextInt();
    System.out.println("Write how many disposable cups of coffee you want to add: ");
    fillDisposableCups = input.nextInt();

    MainMenu();
}

static void BuyCoffee() {                                  //method for buying coffee
    disposableCups--;// always decreasing when coffee is sold
    class TypesOfCoffee {
        void Espresso() {                             //nested class, since there are different kind of coffee
            espressoWater = 250;
            espressoMilk = 0;
            espressoCoffeeBeans = 16;
            espressoCost = 4;


            if (remWater > espressoWater && remMilk > espressoMilk && remCoffeeBean > espressoCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < espressoWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remCoffeeBean < espressoCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void Latte() {
            latteWater = 350;
            latteMilk = 75;
            latteCoffeeBeans = 20;
            latteCost = 7;

            if (remWater > latteWater && remMilk > latteMilk && remCoffeeBean > latteCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < latteWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < latteMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < latteCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void Cappuccino() {
            cappuccinoWater = 200;
            cappuccinoMilk = 100;
            cappuccinoCoffeeBeans = 12;
            cappuccinoCost = 6;

            if (remWater > cappuccinoWater && remMilk > cappuccinoMilk && remCoffeeBean > cappuccinoCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < cappuccinoWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < cappuccinoMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < cappuccinoCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }

        void regularCoffee() {
            regWater = 200;
            regMilk = 50;
            regCoffeeBeans = 15;
            regCost = 0;

            if (remWater > regWater && remMilk > regMilk && remCoffeeBean > regCoffeeBeans) {
                System.out.println("I have enough resources, making you a coffee!");
            } else if (remWater < regWater) {
                System.out.println("Sorry, not enough water!");
                disposableCups++;
            } else if (remMilk < regMilk) {
                System.out.println("Sorry, not enough milk!");
                disposableCups++;
            } else if (remCoffeeBean < regCoffeeBeans) {
                System.out.println("Sorry, not enough coffee beans!");
                disposableCups++;
            } else
                disposableCups++;
        }
    }

    Scanner input = new Scanner(System.in);
    System.out.println("What kind of coffee would you like to buy today?");
    System.out.println("""
            Please enter:
            1 for espresso
            2 for latte
            3 for cappuccino
            4 for a regular coffee
            back - to go back to main menu""");
    String choice = input.next();

    switch (choice) { //lets user choose what type of coffee to buy
        case "1":
            new TypesOfCoffee().Espresso();
            break;
        case "2":
            new TypesOfCoffee().Latte();
            break;
        case "3":
            new TypesOfCoffee().Cappuccino();
            break;
        case "4":
            new TypesOfCoffee().regularCoffee();
            break;
        case "back":
            MainMenu();
            break;
        default:
            System.out.println("Please enter an option from the above");
    }
    MainMenu();
}


static void takeMoney() { //method to take the money
    System.out.println("I gave you" + " " + "$" + remCost);
    newRemCost = remCost;
    MainMenu();
}

static void getRemainingResources() {
    //TODO --has to loop..but it works only once
    remDisposableCups = fillDisposableCups + disposableCups;
    boolean found = remDisposableCups > 0; // true for coffee to be sold
    while (found) {
        remWater = water + fillWater - espressoWater - cappuccinoWater - latteWater;//
        remMilk = milk + fillMilk - espressoMilk - cappuccinoMilk - latteMilk;
        remCoffeeBean = coffeeBean + fillCoffeeBeans - espressoCoffeeBeans - cappuccinoCoffeeBeans - latteCoffeeBeans;

        remCost = remCost + espressoCost + cappuccinoCost + latteCost - newRemCost;
        espressoCost = 0;
        cappuccinoCost = 0;
        latteCost = 0;
        //found = remDisposableCups > 0 && remWater>=water && remMilk>=milk&&remCoffeeBean>=coffeeBean;

        System.out.println("The coffee machine has: ");
        System.out.println(remWater + " " + "ml of water");
        System.out.println(remMilk + " " + "ml of milk ");
        System.out.println(remCoffeeBean + " " + "g of coffee beans ");
        System.out.println(remDisposableCups + " " + "disposable cups ");
        System.out.println("$" + remCost + " " + "of money");
        found = remDisposableCups < 0;
    }

    MainMenu();
}

static void MainMenu() { // Gives user option to decide what they want to do
    Scanner input = new Scanner(System.in);
    System.out.println("Main Menu: " + "\n");
    System.out.println("Please choose from the following options below");
    System.out.println("""
            To buy coffee - Enter buy
            To fill the machine - Enter fill
            To get cash from machine - Enter take
            To see remaining resources - Enter remaining
            To exit program - Enter exit""");
    String choice = input.next();

    switch (choice) {
        case "buy":
            BuyCoffee();
            break;
        case "fill":
            FillCoffeeMachine();
            break;
        case "take":
            takeMoney();
            break;
        case "remaining":
            getRemainingResources();
            break;
        case "exit":
            System.exit(0);
            break;
        default:
            MainMenu();//goes back to main menu if user types in an unknown value
     }
}

public static void main(String[] args) {
    MainMenu();
}
}

你写的代码非常复杂,所以你看不出错误在哪里。一些评论:

  • Java 是一种面向对象 (OO) 的语言。 OO 范式围绕创建 classes 及其对象、继承和多态性(从父 classes 继承行为,但在每个子 class 中表现不同)。
  • 您的代码看起来像是在尝试像旧版本的 Basic 或 Macro Assembler 一样重新创建结构化编程,只是遵守 Java 的语法。您需要真正了解 OO 编程的工作原理,因此首先尝试查找一些相关教程。
  • 不要把所有的东西都放在一起class。尝试识别您的应用程序中的不同实体。例如:CoffeMachine、Coffee、Espresso、Latte、RegularCoffee,...为每个已识别的实体创建一个 class。彼此相似的 classes 可能应该是继承的一部分,例如 Espresso、Lattte 等应该继承更通用的 class Coffee(A 继承 B,当你可以说“A is a有点 B").
  • 不要使用静态变量(除非有很好的理由,但这种情况很少见)。当您创建它们的对象时,让每个 class 都有自己的实例属性:remainingWater、remainingMilk 等是对象 CoffeeMachine 的属性。它们不是全球性的东西。
  • 不要使用 C 或 Basic 中的子例程等静态方法。相反,创建 classes 的对象并调用它们的实例方法。
  • 不要像在 Basic 或 Assembler 中那样使用跳转和标签。相反,在循环中调用对象的方法并在必要时中断循环。
  • 如果您在应用程序中检测到错误,请使用 Java 的异常机制 - 抛出异常并让调用者处理错误。
  • 方法名以Java中的小写字母开头(这是公认的约定,不是句法规则)。

我根据上述原则为您的应用程序创建了不同的设计,因此请先从教程中学习面向对象编程的基础知识,然后将以下代码与您的代码进行比较,看看有什么不同。

Main class(主菜单循环的入口点):

public class Main
{
    public static void main(String[] args)
    {
        new Main().run();
    }
    
    void run()
    {
        CoffeeMachine coffeeMachine = new CoffeeMachine(400, 540, 120, 9, 550);
        while (coffeeMachine.mainMenu()); // loops until mainMenu returns false
    }

}

咖啡机:

import java.util.Scanner;

public class CoffeeMachine
{
    private Scanner input = new Scanner(System.in);
    
    private int water;
    private int milk;
    private int coffeeBeans;
    private int disposableCups;
    private int money;
    
    public CoffeeMachine(int water, int milk, int coffeeBeans, int disposableCups, int money)
    {
        this.water = water;
        this.milk = milk;
        this.coffeeBeans = coffeeBeans;
        this.disposableCups = disposableCups;
        this.money = money;
    }

    void fill() {
        System.out.println("Write how many ml of water you want to add: ");
        water += input.nextInt();
        System.out.println("Write how many ml of milk you want to add: ");
        milk += input.nextInt();
        System.out.println("Write how many grams of coffee beans you want to add: ");
        coffeeBeans += input.nextInt();
        System.out.println("Write how many disposable cups for coffee you want to add: ");
        disposableCups += input.nextInt();
        
    }

    public void selectCoffee()
    {
        while (true)
        {
            System.out.println("What kind of coffee would you like to buy today?");
            System.out.println(
        "            Please enter:\n" +
        "            1 for espresso\n" +
        "            2 for latte\n" +
        "            3 for cappuccino\n" +
        "            4 for a regular coffee\n" +
        "            back - to go back to main menu");
            String choice = input.next();
    
            try {
                switch (choice) { //lets user choose what type of coffee to buy
                    case "1":
                        buy(new Espresso());
                        return;
                    case "2":
                        buy(new Latte());
                        return;
                    case "3":
                        buy(new Cappuccino());
                        return;
                    case "4":
                        buy(new RegularCoffee());
                        return;
                    case "back":
                        return;
                    default:
                        System.err.println("Please enter an option from the above");
                }
            }
            catch (InsufficientResourcesException exception) {
                System.err.println("Error buying coffee: " +  exception.getLocalizedMessage());
            }
        }
    }
    
    public void buy(Coffee coffee) throws InsufficientResourcesException
    {
        if (coffee.getWaterCost() > water)
            throw new InsufficientResourcesException("water", "ml", coffee.getWaterCost(), water);

        if (coffee.getMilkCost() > milk)
            throw new InsufficientResourcesException("milk", "ml", coffee.getMilkCost(), milk);

        if (coffee.getCoffeeBeansCost() > coffeeBeans)
            throw new InsufficientResourcesException("coffee beans", "g", coffee.getCoffeeBeansCost(), coffeeBeans);

        if (disposableCups < 1)
            throw new InsufficientResourcesException("disposable cups", "units", 1, disposableCups);
        

        System.out.println("Buying 1 cup of " + coffee.getClass().getSimpleName());
        water -= coffee.getWaterCost();
        milk -= coffee.getMilkCost();
        coffeeBeans -= coffee.getCoffeeBeansCost();
        disposableCups--;
        
        money += coffee.getMoneyCost();
    }
    
    public void takeMoney()
    {
        System.out.println("I gave you $" + money + ". The money tray is now empty.");
        money = 0;
    }
    
    public void showResources()
    {
        System.out.println("The coffee machine has: ");
        System.out.println(water + " ml of water");
        System.out.println(milk + " ml of milk ");
        System.out.println(coffeeBeans + " g of coffee beans ");
        System.out.println(disposableCups + " disposable cups ");
        System.out.println("$" + money + " of money");
    }

    public boolean mainMenu() { // Gives user option to decide what they want to do
        System.out.println("Main Menu: " + "\n");
        System.out.println("Please choose from the following options below");
        System.out.println(
    "            To buy coffee - Enter buy\n" +
    "            To fill the machine - Enter fill\n" +
    "            To get cash from machine - Enter take\n" +
    "            To see remaining resources - Enter remaining\n" +
    "            To exit program - Enter exit");
        String choice = input.next();
        
        switch (choice) {
            case "buy":
                selectCoffee();
                break;
            case "fill":
                fill();
                break;
            case "take":
                takeMoney();
                break;
            case "remaining":
                showResources();
                break;
            case "exit":
                return false;
        }
        
        return true;
    }

}

还有咖啡 classes:

public abstract class Coffee
{
    private int waterCost;
    private int milkCost;
    private int coffeeBeansCost;
    private int moneyCost;
    
    protected Coffee(int waterCost, int milkCost, int coffeeBeansCost, int moneyCost)
    {
        this.waterCost = waterCost;
        this.milkCost = milkCost;
        this.coffeeBeansCost = coffeeBeansCost;
        this.moneyCost = moneyCost;
    }

    public int getWaterCost()
    {
        return this.waterCost;
    }

    public int getMilkCost()
    {
        return this.milkCost;
    }

    public int getCoffeeBeansCost()
    {
        return this.coffeeBeansCost;
    }

    public int getMoneyCost()
    {
        return this.moneyCost;
    }
    
}

public class Espresso extends Coffee
{

    public Espresso()
    {
        super(250, 0, 16, 4);
    }

}

public class Cappuccino extends Coffee
{

    public Cappuccino()
    {
        super(200, 100, 12, 6);
    }

}

public class Latte extends Coffee
{

    public Latte()
    {
        super(350, 75, 20, 7);
    }

}

public class RegularCoffee extends Coffee
{

    public RegularCoffee()
    {
        super(200, 50, 15, 0);
    }

}

最后但同样重要的是,异常 class:

public class InsufficientResourcesException extends Exception
{
    private static final long serialVersionUID = 1L;

    public InsufficientResourcesException(String whatResource, String unit, int required, int available)
    {
        super("Insufficient resources: required " + required + " " + unit + " of " + whatResource + " but available " + available + " " + unit);
    }
}

拥有一个好的架构在软件中是最重要的。如果您的架构应用得当,错误就不太可能发生,即使发生也很容易修复。此外,更改具有良好架构的应用程序既简单又安全。

另一方面,糟糕的架构使得错误几乎是必然的,很难找到和修复,而且应用程序也不容易更改。