ボタンを押すたびに積算合計を計算したいと思います。数量+合計数量(ボタンがすでに押されている場合)を加算する必要があります。以下は私のコードスニペットです。上記のコード、エラーでコードネーム1:各ボタンの繰り返しの合計を計算するには?
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 {
...
}
});
(「ラムダ式から参照されるローカル変数は、最終又は最終有効でなければならない」)以下のコードのために発生します。私はまた、「公共のボイドのactionPerformed()」メソッドを試してみましたが、その結果、ノー成功
total += num2;
Dialog.show("", "You have consumed a total of " + Integer.toString(total) + "ml of water", "OK", "Close");
で、私は、実行中の合計をコーディングする方法上の任意の助けや指導をお願い申し上げます。