英文:
How can I add content of different rows of my jTable in a variable?
问题
抱歉,如果我的问题看起来很愚蠢,但我真的不擅长使用For循环。我的窗口如下所示。
当我点击“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.
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 < 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 < jTable4.getRowCount(); i++) { // Loop through the rows
total += (float) jTable4.getValueAt(i, 2);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论