动态更新值 ?
所以我必须编写一个程序来获取用户的公式并进行计算。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们可以修改它来制作一个简单的引擎
有了这个,我们就可以定义各种函数和计算
当您尝试使其不仅是用户定义的,而且是用户输入的时,问题就出现了。例如,用户可以将函数定义为您预先输入的一组函数之一。在这种情况下,您需要一个解析器将这样的内容转换
为:
但这可能超出了您的作业范围。或者也许不是。
希望这比我之前的示例更接近您的要求。
We can revise this to make a simple engine
With this in place, we can define various functions and calculations
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:
into this:
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.
您可能想考虑使用多种表达式计算引擎之一来代替 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.
您可能想为这些数组编写一个事件侦听器。文档可以在这里找到
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.
说实话我会用更多的OO方式,请看一下:
我也会让
Parameter
和Equation
对象不可变,如果有必要改变我会创建另一个方程对象,但如果这是要求,您可以在Equation
中编写方法来添加/设置参数
。如果它们具有相同的 id,Set
将正确处理它们。To be honest I'll use more OO manner, please take a look:
I would also make
Parameter
andEquation
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 inEquation
for adding/settingParameter
's.Set
will handle them properly if they will have same id's.