动态更新值 ?

发布于 2024-12-06 09:54:01 字数 299 浏览 0 评论 0原文

所以我必须编写一个程序来获取用户的公式并进行计算。

    Age, Height, fights, (age+height)*fights <- user defined
    10, 5, 10, 150 <- I calculate this 
    1, 2, 1, 3 <- I calculate this 

但现在假设我更改了周围的值,并且我希望公式列动态更新,是否可以这样做?我将每一行存储到一个数组列表中,该数组列表是数组列表的数组列表。任何建议或指导都会非常有帮助,谢谢:)

So I have to write a program that will take formulas from the user and calculate them.

    Age, Height, fights, (age+height)*fights <- user defined
    10, 5, 10, 150 <- I calculate this 
    1, 2, 1, 3 <- I calculate this 

But now let say I change the values around and I want the formula column to update dynamically is there anyway to go about doing that ? I am storing by each row into an array list which is array list of array list. Any advice or guidance would be really helpful thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

櫻之舞 2024-12-13 09:54:01

我们可以修改它来制作一个简单的引擎

public interface Function<T> {
    T operate(Map<String,T> values);
}

public class Calculation<T> {

     private Map<String,T> values;

     private Function<T> function;

     public Calculation(Map<String,T> values, Function<T> function) {
         this.values = new HashMap<String,T>(values);
         this.function = function;
     }

     public T calculate() {
         return function.operate(Collections.unmodifiableMap(values));
     }

     // assume setters and getters are in place to manipulate the backing map and the functor object

}

有了这个,我们就可以定义各种函数和计算

public static void main(String[] args) {
    Function<Integer> original = new Function<Integer>() {
        public Integer operate(Map<String,Integer> values) {
            // these will throw exceptions if they don't exist, which is desired
            // granted it's NullPointerException instead of IllegalStateException, but close enough for this example
            int age = values.get("age").intValue();
            int height = values.get("height").intValue();
            int fights = values.get("fights").intValue();
            return (age+height)*fights;
        }
    };
    Map<String,Integer> map = new Map<String,Integer>();
    map.put("age",10);
    map.put("height",100);
    map.put("fights",25);
    Calculation<Integer> calc = new Calculation<Integer>(map,original);

    // later someone can replace either the values in the backing map
    // or replace the function altogether to get a new function
}

当您尝试使其不仅是用户定义的,而且是用户输入的时,问题就出现了。例如,用户可以将函数定义为您预先输入的一组函数之一。在这种情况下,您需要一个解析器将这样的内容转换

value + (other + (something * that)) / wazook

为:

EquationBuilder<Integer> eb = new EquationBuilder<Integer>();
eb.append(map.get("value"));
eb.append(OPERATOR.PLUS);
// etc
return eb.evaluate();

但这可能超出了您的作业范围。或者也许不是。

希望这比我之前的示例更接近您的要求。

We can revise this to make a simple engine

public interface Function<T> {
    T operate(Map<String,T> values);
}

public class Calculation<T> {

     private Map<String,T> values;

     private Function<T> function;

     public Calculation(Map<String,T> values, Function<T> function) {
         this.values = new HashMap<String,T>(values);
         this.function = function;
     }

     public T calculate() {
         return function.operate(Collections.unmodifiableMap(values));
     }

     // assume setters and getters are in place to manipulate the backing map and the functor object

}

With this in place, we can define various functions and calculations

public static void main(String[] args) {
    Function<Integer> original = new Function<Integer>() {
        public Integer operate(Map<String,Integer> values) {
            // these will throw exceptions if they don't exist, which is desired
            // granted it's NullPointerException instead of IllegalStateException, but close enough for this example
            int age = values.get("age").intValue();
            int height = values.get("height").intValue();
            int fights = values.get("fights").intValue();
            return (age+height)*fights;
        }
    };
    Map<String,Integer> map = new Map<String,Integer>();
    map.put("age",10);
    map.put("height",100);
    map.put("fights",25);
    Calculation<Integer> calc = new Calculation<Integer>(map,original);

    // later someone can replace either the values in the backing map
    // or replace the function altogether to get a new function
}

The problem comes when you try to make it not only user defined, but user entered. As in, a user could define the function as being one of a set of functions you have pre-entered. In this case, you'll need to have a parser that turns something like this:

value + (other + (something * that)) / wazook

into this:

EquationBuilder<Integer> eb = new EquationBuilder<Integer>();
eb.append(map.get("value"));
eb.append(OPERATOR.PLUS);
// etc
return eb.evaluate();

But that may be beyond the scope of your assignment. Or maybe it's not.

Hopefully this is a little closer to your requirements than my previous example.

岁月静好 2024-12-13 09:54:01

您可能想考虑使用多种表达式计算引擎之一来代替 ArrayList。您可以查看 Expr4J 或 BIRT 以获取可能的解决方案。

You might want to look at using one of a number of expression calculation engines instead of ArrayLists. You can see Expr4J or BIRT for possible solutions.

黑凤梨 2024-12-13 09:54:01

您可能想为这些数组编写一个事件侦听器。文档可以在这里找到
http://download.oracle.com/javase/tutorial/uiswing/events /listdatalistener.html

一个更简单的解决方案是编写一个简单的方法来计算公式并从触发列表值更改的程序的每个部分调用它(但此解决方案可能会使代码混乱)。如果这是一个家庭作业或非常小的项目,您可以采用这种方法,否则我建议编写一个侦听器。

You might want to write an event listener for those arrays. Documentation can be found here
http://download.oracle.com/javase/tutorial/uiswing/events/listdatalistener.html

An easier solution is to write a simple method that computes the formula and call it from each portion of the program that triggers a change in the list values (but this solution can clutter the code). If this is a homework or really small project you can go with this approach otherwise I would suggest writing a listener.

瑕疵 2024-12-13 09:54:01

说实话我会用更多的OO方式,请看一下:

public class Parameter {
    private String id;
    private Number value;

    public Parameter(String id, Number value) {
        this.id = id;
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public Number getValue() {
        return value;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Parameter) {
            Parameter parameter = (Parameter) obj;
            return id.equals(parameter.getId());
        }

        return super.equals(obj);
    }

}

public class Equation {
    private String formula;
    private Set<Parameter> parameters;

    public Equation(String formula, Parameter ... parameters) {
        this.formula = formula;
        this.parameters = new HashSet<Parameter>();
        for(Parameter parameter : parameters) {
            this.parameters.add(parameter);
        }
        validatePresenceOfAllParameters();
    }

    private void validatePresenceOfAllParameters() {
        // here you parse formula and check if all parameters are present
    }

    public Number doTheMagic() {
        Number result = null;
        // here you do the all magic related to equation and its parameters
        return result;
    }

    // public void setParameter(Parameter parameter)  {
    //  parameters.add(parameter);
    //  validatePresenceOfAllParameters();
    // }

}

我也会让ParameterEquation对象不可变,如果有必要改变我会创建另一个方程对象,但如果这是要求,您可以在Equation中编写方法来添加/设置参数。如果它们具有相同的 id,Set 将正确处理它们。

To be honest I'll use more OO manner, please take a look:

public class Parameter {
    private String id;
    private Number value;

    public Parameter(String id, Number value) {
        this.id = id;
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public Number getValue() {
        return value;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Parameter) {
            Parameter parameter = (Parameter) obj;
            return id.equals(parameter.getId());
        }

        return super.equals(obj);
    }

}

public class Equation {
    private String formula;
    private Set<Parameter> parameters;

    public Equation(String formula, Parameter ... parameters) {
        this.formula = formula;
        this.parameters = new HashSet<Parameter>();
        for(Parameter parameter : parameters) {
            this.parameters.add(parameter);
        }
        validatePresenceOfAllParameters();
    }

    private void validatePresenceOfAllParameters() {
        // here you parse formula and check if all parameters are present
    }

    public Number doTheMagic() {
        Number result = null;
        // here you do the all magic related to equation and its parameters
        return result;
    }

    // public void setParameter(Parameter parameter)  {
    //  parameters.add(parameter);
    //  validatePresenceOfAllParameters();
    // }

}

I would also make Parameter and Equation object immutable, if there is a necessity of change I'll make another equation object but if this is the requirement you could write method in Equation for adding/setting Parameter's. Set will handle them properly if they will have same id's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文