如何在特定场景实现按值传递?

huangapple go评论66阅读模式
英文:

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

问题

我在一个项目中遇到了一个轻微的问题即在buyOption”、“fillOptiontakeOption中实现按值传递

我不知道如何使改变的值适用于整个程序而不仅仅适用于方法如果有任何提示解决方案或反馈将不胜感激

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;
    }
}
英文:

I'm having a slight problem implementing pass-by-value in a project. In case of buyOption, fillOption and takeOption.

I don't know how to make changed value apply to whole program and not only to methods. Any tips/solution/feedback would be highly appreciated.

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(&quot;Write action (buy, fill, take, remaining, exit): &quot;);
String input = scanner.nextLine();
switch (input) {
case &quot;buy&quot;:
buyOption(water, milk, beans, cups, money);
break;
case &quot;fill&quot;:
fillOption(water, milk, beans, cups, money);
break;
case &quot;take&quot;:
takeOption(water, milk, beans, cups, money);
break;
case &quot;remaining&quot;:
contentOption(water, milk, beans, cups, money);
break;
case &quot;exit&quot;:
exit = true;
System.exit(0);
break;
default:
System.out.println(&quot;Wrong option!&quot;);
break;
}
}
private static void contentOption(int water, int milk, int beans, int cups, int money) {
System.out.println(&quot;The coffee machine has: &quot;);
System.out.println(water + &quot; of water&quot;);
System.out.println(milk + &quot; of milk&quot;);
System.out.println(beans + &quot; of coffee beans&quot;);
System.out.println(cups + &quot; of disposable cups&quot;);
System.out.println(money + &quot; of money&quot;);
}
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(&quot;What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: &quot;);
String input = scanner.nextLine();
switch (input) {
case &quot;1&quot;:
if (water &gt;= 250 &amp;&amp; beans &gt;= 16 &amp;&amp; cups &gt;= 1) {
water -= 250;
beans -= 16;
cups -= 1;
money += 4;
System.out.println(&quot;I have enough resources, making you a coffee!&quot;);
} else {
if (water &lt; 250) {
temp = &quot;water&quot;;
} else if (beans &lt; 16) {
temp = &quot;beans&quot;;
} else if (cups &lt; 1) {
temp = &quot;cups&quot;;
}
System.out.println(&quot;Sorry, not enough &quot; + temp + &quot;!&quot;);
}
break;
case &quot;2&quot;:
if (water &gt;= 350 &amp;&amp; milk &gt;= 75 &amp;&amp; beans &gt;= 20 &amp;&amp; cups &gt;= 1) {
water -= 350;
milk -= 75;
beans -= 20;
cups -= 1;
money += 7;
System.out.println(&quot;I have enough resources, making you a coffee!&quot;);
} else {
if (water &lt; 350) {
temp = &quot;water&quot;;
} else if (milk &lt; 75) {
temp = &quot;milk&quot;;
} else if (beans &lt; 20) {
temp = &quot;beans&quot;;
} else if (cups &lt; 1) {
temp = &quot;cups&quot;;
}
System.out.println(&quot;Sorry, not enough &quot; + temp + &quot;!&quot;);
}
break;
case &quot;3&quot;:
if (water &gt;= 200 &amp;&amp; milk &gt;= 100 &amp;&amp; beans &gt;= 12 &amp;&amp; cups &gt;= 1) {
water -= 200;
milk -= 100;
beans -= 12;
cups -= 1;
money += 6;
System.out.println(&quot;I have enough resources, making you a coffee!&quot;);
} else {
if (water &lt; 200) {
temp = &quot;water&quot;;
} else if (milk &lt; 100) {
temp = &quot;milk&quot;;
} else if (beans &lt; 12) {
temp = &quot;beans&quot;;
} else if (cups &lt; 1) {
temp = &quot;cups&quot;;
}
System.out.println(&quot;Sorry, not enough &quot; + temp + &quot;!&quot;);
}
break;
case &quot;back&quot;:
break;
default:
System.out.println(&quot;Wrong option!&quot;);
}
}
private static void fillOption(int water, int milk, int beans, int cups, int money) {
Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Write how many ml of water do you want to add: &quot;);
water = water + scanner.nextInt();
System.out.println(&quot;Write how many ml of milk do you want to add: &quot;);
milk = milk + scanner.nextInt();
System.out.println(&quot;Write how many grams of coffee beans do you want to add: &quot;);
beans = beans + scanner.nextInt();
System.out.println(&quot;Write how many disposable cups of coffee do you want to add: &quot;);
cups = cups + scanner.nextInt();
}
private static void takeOption(int water, int milk, int beans, int cups, int money) {
System.out.println(&quot;I gave you &quot; + money);
money = 0;
}
}

答案1

得分: 3

Java始终按值传递。问题在于你正在使用局部变量(每个方法中声明的参数),这些局部变量隐藏了你的静态成员。
你可以简单地在方法声明中省略所有参数:这样你将会“使用”你在开头声明的静态成员。

编辑

private static void takeOption(int water, int milk, int beans, int cups, int money) {
//示范代码
milk = milk+1;
}

在这种情况下,你使用的每个操作,比如“milk”,只会存在于这个方法内部。你的参数被命名为milk,但它与在类顶部声明的“milk”没有任何关系。它只是一个巧合它们被命名为相同的名字。
你可以使用this.milk(或CoffeeMachine.milk,因为它是一个静态字段)来引用你的static milk,但从你目前的代码来看,我看不出在不进一步编辑你的方法的情况下有什么用处。

private static void takeOption() {
//在这里编写代码
}

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

英文:

Java is always pass-by-value. The problem here is that you are working with local variables (the parameters declared in every method) which are hiding your static members.
You can simply omit all the parameters in method declaration: this way you will "use" the static members you declared at the beginning.

EDIT

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

In this scenario, every operation you make using, let's say, "milk" will only exist inside this method. Your parameter is named milk, but it has nothing to share with "milk" declared on top of your class. It is just a coincidence that they are named the same.
You cann refer to your static milk using this.milk (or CoffeeMachine.milk, since it is a static field), but as your code is written at the moment, I can see no use without editing your methods furthermore.

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

Editing your method signature like this will make you use your static variables.

答案2

得分: 1

为了影响您为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!");
}
英文:

In order to affect the static variables you have set for your CoffeeMachine.

You have to reference the static variable directly inside your method:

So instead of just using

water -= 250;

Which affects only the parameter (local scope)

You will have to use:

CoffeeMachine.water -= 250;

As it has been pointed out, the fields shouldn't be static, but I'm assuming you are learning java at this moment.

NOTE:
For your purposes probably you don't need parameters in your method as you are affecting the initial static variables, but in case you actually needed parameters:

You could change the names of your parameters:

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

Could have easily just become:

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

With the new names you could just use the variable water to represent your static variable while using localWater to represent your parameter

Then this would work:

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

huangapple
  • 本文由 发表于 2020年10月2日 20:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64171807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定