【发布时间】:2015-09-29 15:47:31
【问题描述】:
场景 :
JTable 包含以下数据,我试图在下图中描述我想要做的事情:-
所以,我想我可以在这里解释我想要实现的目标。
面临的问题 当然,不显示准确的结果(总和)。我使用的代码是:
public void docTotal_Income(){
try{
int totC=8,xC=3,lC=4,eC=5, sC=6; // totC is the last column, xC-3rd, lC-4th and so on...
for(int i=0;i<(easypath.doctorBusiness_table.getRowCount());i++){ // "easypath.doctorBusiness_table" is the table name
sumTot += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, totC).toString());
sumTotx += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, xC).toString());
sumTotl += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, lC).toString());
sumTote += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, eC).toString());
sumTots += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, sC).toString());
}
easypath.totalEarnt_docBus_tf.setText(String.valueOf(sumTot));
easypath.xTotIncome_tf.setText(String.valueOf(sumTotx));
easypath.lTotIncome_tf.setText(String.valueOf(sumTotl));
easypath.eTotIncome_tf.setText(String.valueOf(sumTote));
easypath.sTotIncome_tf.setText(String.valueOf(sumTots));
sumTot = 0; // public static
sumTotx = 0; // values globally
sumTotl = 0; // declared
sumTote = 0; // and
sumTots = 0; // initialised 0
}
catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error in totalling income");
}
}
我在用 Document Listener 精炼 JTable 之后调用方法 docTotal_Income()(工作正常)并最终触发eventListener 上的 JButton。
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
new doctor().docTotal_Income(); // doctor is the class
}
在所有这些之后,我得到了不规则的总和。我猜我在逻辑的某个地方出了问题,但我还有什么遗漏的吗?
我很乐意对此提出任何建议。感谢您的宝贵时间
【问题讨论】:
-
什么是“不规则求和”?你的总数减少了 0.01 吗?减 1,000,000?总数是NaN?无穷大?
-
不规则并不意味着尺寸或任何错误。我试图解释它给出了错误的求和,5+5 = 20 等等....我猜这是我的 逻辑错误,但我无法指出,所以放整个场景按此顺序
-
将数字放入 XxxTableModel,覆盖模型定义中的 getColumnClass
标签: java swing jtable documentlistener