在JTable列中查找值的总和

Ash*_*hok 0 java swing

我想在JTable中的列中找到值的总和.我已经使用getvalueAt方法从JTable中检索了值,但它最终没有找到值的总和.这是我的代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    for(int i = 0; i <model1.getRowCount(); i++) {
        int total =0;
        int Amount =Integer.parseInt(model1.getValueAt(i,3)+"");
        total +=Amount;
        System.out.println(total);
     }
}
Run Code Online (Sandbox Code Playgroud)

我目前的输出是这样的:

10 // values in column
20
30
Run Code Online (Sandbox Code Playgroud)

Men*_*ena 5

totalfor循环中声明,因此0在每次迭代时都会初始化.

for循环之前声明它.