简体   繁体   English

如何根据用户输入设置静态最终变量?

[英]How should I set a static final variable based on user input?

I am trying to model the classic water jug problem in AI. 我正在尝试为AI中的经典水壶问题建模。 I have made a class "JugsState" that stores the current state of two jugs, ie how many liter of water is in jug1 and how many liter of water in jug2. 我创建了一个“ JugsState”类,该类存储两个水罐的当前状态,即jug1中有多少升水,jug2中有多少升水。 In addition, I want to store the maximum amount of water each jug can hold, which I will take from the user as input. 另外,我想存储每个水罐可以容纳的最大水量,我将从用户那里取水作为输入。 Since this(capacity of the two jugs) will be constant throughout , I am declaring them as static final variables. 由于这(两个水罐的容量)在整个过程中都是恒定的,因此我将它们声明为静态最终变量。 But I am unable to initialize them inside the constructor. 但是我无法在构造函数中初始化它们。 Is there any other alternative to this, which maintains the encapsulation of max_jug variables inside the class JugsState? 除此之外,还有其他替代方法可以将max_jug变量封装在类JugsState中吗?

class JugsState
{
    private static final int max_jug1,max_jug2;
    private int jug1,jug2; //stores the current amount of water in the jugs.

    JugsState(int a1,int a2)
    {
        max_jug1 = a1;
        max_jug2 = a2;
    }
}

error: "cannot assign a value to final variable max_jug1" error: "cannot assign a value to final variable max_jug2" 错误:“无法将值分配给最终变量max_jug1”错误:“无法将值分配给最终变量max_jug2”

If your class represents a jug, it should not hold information about two jugs. 如果您的班级代表一个水罐,则不应保存有关两个水罐的信息。 maxAmount or volume should be a non-static member of the class: maxAmountvolume应该是该类的非静态成员:

public class Jug {
    public final double volume;
    private double currentAmount = 0;
    public Jug(double vol) { volume = vol; }
    ...
}

You can't change a final variable because it is final . 您不能更改final变量,因为它是final However, you can set it to anything you want when it is declared. 但是, 可以在声明它时将其设置为所需的任何值。 You can create a static function that gets the maximum for a jar. 您可以创建一个静态函数来获取罐子的最大值。 It could be something like this, if you want to get it from System.in: 如果要从System.in获取它,可能是这样的:

private static int getMax(){
    System.out.println("Enter the maximum for a jar:");
    Scanner in=new Scanner(System.in);
    return in.nextInt();
}

Then use 然后使用

private static final int max_jug1=getMax();
private static final int max_jug2=getMax();

in place of 代替

private static final int max_jug1, max_jug2;

That will set those variables for the rest of the time that the program runs. 这将在程序运行的其余时间内设置这些变量。

If a variable is static , then it would be re-initialized every time the constructor is called. 如果变量是static ,则每次调用构造函数时,它将重新初始化。 If it is final it can be set only once. 如果是final ,则只能设置一次。 Something close to what you described could be the following (notice the static method setMaxJug1 ) 可能与您描述的内容setMaxJug1 (请注意静态方法setMaxJug1

   class jug
    {
        private static int max_jug1,max_jug2;
        private int jug1,jug2; //stores the current amount of water in the jugs.
        jug(){
          // Your constructor stuff
        }
        public static void setMaxJug1(int m){
           max_jug1 = m;
        }
    }

Then in the main you can call 然后你可以打电话

 jug.setMaxJug1(m);

Probably there are other ways of doing what you ask, but I tried not to change too much of your code. 可能还有其他方法可以执行您的要求,但我尝试不更改过多的代码。

Just to mention, one alternative is to have those variables not static, as in: 只需提及,一种选择是使这些变量不是静态的,如:

class jug
        {
            private final int max_jug1,max_jug2;
            private int jug1,jug2; //stores the current amount of water in the jugs.
            jug(int a1,int a2)
             {
               max_jug1 = a1;
               max_jug2 = a2;
             }   
        }

Then you could use your original constructor. 然后,您可以使用原始的构造函数。 However, every jug Object will have (in general) a different value for max_jug1 and max_jug2 , depending on the values ( a1,a2 ) you pass in the constructor. 但是,根据您在构造函数中传递的值( a1,a2 ),每个jug Object的max_jug1max_jug2值通常不同。

The right choice depends on how you want to use jug objects. 正确的选择取决于您要如何使用水罐对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM