[英]Finding sum of values in a JTable column
我想在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);
}
}
我當前的輸出越來越像這樣:
10 // values in column
20
30
您在for
循環中聲明total
,因此每次迭代將其初始化為0
。
在for
循環之前聲明它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.