如何在变量中添加 jTable 不同行的内容?
How can I add content of different rows of my jTable in a variable?
抱歉,如果我的问题看起来很愚蠢,但我不太擅长 For 循环。
我的 window 看起来像这样。
jFrame
当我点击“valider”时,我想将 Prix 列中的每一行添加到一个变量 Total 中。
这是我的循环:
float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++)
total =+ (float) jTable4.getValueAt( i, 2);
当我检查 Total 变量中的内容时,它只给出了最后一行的内容。
你们能帮我解决这个循环吗?
问题是您使用了无效的赋值运算符。 =+ 应更改为 += 以获得预期的答案。
float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++) { // Loop through the rows
total += (float) jTable4.getValueAt(i, 2);
}
抱歉,如果我的问题看起来很愚蠢,但我不太擅长 For 循环。 我的 window 看起来像这样。
jFrame
当我点击“valider”时,我想将 Prix 列中的每一行添加到一个变量 Total 中。 这是我的循环:
float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++)
total =+ (float) jTable4.getValueAt( i, 2);
当我检查 Total 变量中的内容时,它只给出了最后一行的内容。
你们能帮我解决这个循环吗?
问题是您使用了无效的赋值运算符。 =+ 应更改为 += 以获得预期的答案。
float total = 0;
for (int i = 0; i < jTable4.getRowCount(); i++) { // Loop through the rows
total += (float) jTable4.getValueAt(i, 2);
}