如何在选定的场景中实现按值传递?

How do I implement pass-by-value in selected scenario?

我在项目中实现按值传递时遇到一个小问题。在 buyOptionfillOptiontakeOption 的情况下。

我不知道如何将更改后的值应用于整个程序而不仅仅是方法。任何 tips/solution/feedback 将不胜感激。

public class CoffeeMachine {

    static int water = 400;
    static int milk = 540;
    static int beans = 120;
    static int cups = 9;
    static int money = 550;
    static boolean exit = false;

    public static void main(String[] args) {
        do {
            printMenu(water, milk, beans, cups, money);
        } while (!exit);
    }

    private static void printMenu(int water, int milk, int beans, int cups, int money) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Write action (buy, fill, take, remaining, exit): ");
        String input = scanner.nextLine();

        switch (input) {
            case "buy":
                buyOption(water, milk, beans, cups, money);
                break;

            case "fill":
                fillOption(water, milk, beans, cups, money);
                break;

            case "take":
                takeOption(water, milk, beans, cups, money);
                break;

            case "remaining":
                contentOption(water, milk, beans, cups, money);
                break;

            case "exit":
                exit = true;
                System.exit(0);
                break;

            default:
                System.out.println("Wrong option!");
                break;
        }
    }

    private static void contentOption(int water, int milk, int beans, int cups, int money) {
        System.out.println("The coffee machine has: ");
        System.out.println(water + " of water");
        System.out.println(milk + " of milk");
        System.out.println(beans + " of coffee beans");
        System.out.println(cups + " of disposable cups");
        System.out.println(money + " of money");
    }

    private static void buyOption(int water, int milk, int beans, int cups, int money) {
        Scanner scanner = new Scanner(System.in);

        String temp = null;

        System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ");

        String input = scanner.nextLine();

        switch (input) {
            case "1":
                if (water >= 250 && beans >= 16 && cups >= 1) {
                    water -= 250;
                    beans -= 16;
                    cups -= 1;
                    money += 4;
                    System.out.println("I have enough resources, making you a coffee!");
                } else {
                    if (water < 250) {
                        temp = "water";
                    } else if (beans < 16) {
                        temp = "beans";
                    } else if (cups < 1) {
                        temp = "cups";
                    }
                    System.out.println("Sorry, not enough " + temp + "!");
                }
                break;

            case "2":
                if (water >= 350 && milk >= 75 && beans >= 20 && cups >= 1) {
                    water -= 350;
                    milk -= 75;
                    beans -= 20;
                    cups -= 1;
                    money += 7;
                    System.out.println("I have enough resources, making you a coffee!");
                } else {
                    if (water < 350) {
                        temp = "water";
                    } else if (milk < 75) {
                        temp = "milk";
                    } else if (beans < 20) {
                        temp = "beans";
                    } else if (cups < 1) {
                        temp = "cups";
                    }
                    System.out.println("Sorry, not enough " + temp + "!");
                }
                break;

            case "3":
                if (water >= 200 && milk >= 100 && beans >= 12 && cups >= 1) {
                    water -= 200;
                    milk -= 100;
                    beans -= 12;
                    cups -= 1;
                    money += 6;
                    System.out.println("I have enough resources, making you a coffee!");
                } else {
                    if (water < 200) {
                        temp = "water";
                    } else if (milk < 100) {
                        temp = "milk";
                    } else if (beans < 12) {
                        temp = "beans";
                    } else if (cups < 1) {
                        temp = "cups";
                    }
                    System.out.println("Sorry, not enough " + temp + "!");
                }
                break;

            case "back":
                break;

            default:
                System.out.println("Wrong option!");
        }
    }

    private static void fillOption(int water, int milk, int beans, int cups, int money) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Write how many ml of water do you want to add: ");
        water = water + scanner.nextInt();
        System.out.println("Write how many ml of milk do you want to add: ");
        milk = milk + scanner.nextInt();
        System.out.println("Write how many grams of coffee beans do you want to add: ");
        beans = beans + scanner.nextInt();
        System.out.println("Write how many disposable cups of coffee do you want to add: ");
        cups = cups + scanner.nextInt();
    }

    private static void takeOption(int water, int milk, int beans, int cups, int money) {
        System.out.println("I gave you " + money);
        money = 0;
    }
}

Java 总是 pass-by-value。这里的问题是您正在使用隐藏静态成员的局部变量(在每个方法中声明的参数)。 您可以简单地省略方法声明中的所有参数:这样您将“使用”您在开始时声明的静态成员。

编辑

private static void takeOption(int water, int milk, int beans, int cups, int money) {
   //sample code
   milk = milk+1;
}

在这种情况下,您使用比方说“牛奶”进行的每个操作都只会存在于此方法中。您的参数名为 milk,但它与在您的 class 上声明的“milk”没有任何共享。他们的名字相同只是巧合。 您可以使用 this.milk(或 CoffeeMachine.milk,因为它是一个静态字段)来引用您的 static milk,但是由于您目前正在编写代码,所以如果不编辑您的代码,我看不出有什么用方法进一步。

private static void takeOption() {
   //code here
}

像这样编辑方法签名将使您使用静态变量。

为了影响您为 CoffeeMachine 设置的静态变量。

您必须在方法中直接引用静态变量:

所以不只是使用

water -= 250;

只影响参数(局部作用域)

您将不得不使用:

CoffeeMachine.water -= 250;

如前所述,字段不应该是静态的,但我假设您此时正在学习 java。

注意: 出于您的目的,您可能不需要方法中的参数,因为您正在影响初始静态变量,但如果您确实需要参数:

您可以更改参数的名称:

private static void buyOption(int water, int milk, int beans, int cups, int money)

本可以很容易地变成:

private static void buyOption(int localWater, int localMilk, int localBeans, int localCups, int localMoney)

有了新名称,您可以只使用变量 water 来表示您的静态变量,同时使用 localWater代表你的参数

那么这会起作用:

if (localWater >= 250 && localBeans >= 16 && localCups >= 1) {
                water -= 250;
                beans -= 16;
                cups -= 1;
                money += 4;
                System.out.println("I have enough resources, making you a coffee!");
            }