这个 for 循环应该对 TreeMap 中包含的所有值求和。这是有效的,但在内部循环之后,值 up 和 down 用于计算精度。似乎这从未执行过。
我做错了什么?
// for each row
for (Entry<String, TreeMap<String, Integer>> table_row : table.entrySet()) {
// write the label
String row = table_row.getKey() + ",";
int up = 0;
int down = 0;
float accu = 0;
// for each treemap in the row
for (Entry<String, Integer> collumn : table_row.getValue().entrySet()) {
row += collumn.getValue() + ",";
down += collumn.getValue();//this works
if (collumn.getKey() == table_row.getKey()) {
up = collumn.getValue();
}
}
---------> accu = (up / down) * 100; //this is not executed??
System.out.println("t: " + up + " n: " + down + " a: " + accu);
row = row + Float.toString(accu) + " %";
writer.println(row);//this works too but output is always 0%
}
最佳答案
您可能不希望在 if 比较中使用 ==,而是使用 equals()... 除非您希望它们是字符串的同一实例。
关于java - Java中的简单计算问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11748502/