如何将我的jTable不同行的内容添加到一个变量中?

huangapple go评论72阅读模式
英文:

How can I add content of different rows of my jTable in a variable?

问题

抱歉,如果我的问题看起来很愚蠢,但我真的不擅长使用For循环。我的窗口如下所示。

jFrame

当我点击“valider”时,我想将“Prix”列中的每一行都添加到一个变量“Total”中。
这是我的循环:

float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++)
    total += (float) jTable4.getValueAt(i, 2);

当我检查“Total”变量中的内容时,它只给我最后一行的内容。

你们能帮我解决这个循环吗?

英文:

sorry if my question looks stupid but I'm not really good at For loops.
My window looks like this.

jFrame

When I click on "valider", I want to add every rows in the Prix column to a variable Total.
Here is my loop:

float total = 0;
 for (int i = 0; i &lt; jTable4.getRowCount(); i++)
 total =+ (float) jTable4.getValueAt( i, 2);

When I check what's in my Total variable, it just gives me the content of the last row.

Could you guys help me with this loop ?

答案1

得分: 0

问题在于您使用了无效的赋值操作符。=+ 应更改为 += 以获得您期望的结果。

float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++) {  // 遍历行
       
        total += (float) jTable4.getValueAt(i, 2);  
}
英文:

Problem is you have used an invalid assignment Operator . =+ should be change as += to get your expected answer .


float total = 0;
for (int i = 0; i &lt; jTable4.getRowCount(); i++) {  // Loop through the rows
       
        total += (float) jTable4.getValueAt(i, 2);  
}

huangapple
  • 本文由 发表于 2020年10月21日 17:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64460989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定