【发布时间】:2016-04-23 01:20:32
【问题描述】:
我想计算每次按下按钮时的总和。它应该加一个数量加上总数量(如果按钮已经被按下)。以下是我的代码sn-p。
TextField quantity = new TextField("", "Volume of water (ml)", 4, TextArea.ANY);
intake.addComponent(quantity);
Button add = new Button("Add");
intake.addComponent(add);
...
int total = 0;
//--------------------------------------------------------------
add.addActionListener((ActionEvent ev) -> {
Storage s = Storage.getInstance();
try {
String num = quantity.getText();
Integer num2 = Integer.parseInt(num);
String name = itemName.getText();
total += num2;
Dialog.show("", "You have consumed a total of " + Integer.toString(total) + "ml of water", "OK", "Close");
...
} catch {
...
}
});
使用上面的代码,下面的代码 sn-p 会出现错误(“从 lambda 表达式引用的局部变量必须是最终的或有效的最终”)。我也尝试过使用“public void actionPerformed()”方法,但没有成功
total += num2;
Dialog.show("", "You have consumed a total of " + Integer.toString(total) + "ml of water", "OK", "Close");
因此,我将不胜感激有关如何编写运行总计代码的任何帮助和指导。
【问题讨论】:
标签: codenameone cumulative-sum