新
BigDecimal i = BigDecimal.ZERO;
for (int row = 0; row < testMark.length; row++) {
for (int col = 0; col < testMark[row].length; col++) {
i.add(new BigDecimal(testMark[row][col]));
}
}
i.divide(new BigDecimal(60D));
outPut.setText(i.toString());`
OLD
查看代码的底部。我想将整个classAver字符串除以outPut.setText(classAver / 60);
数组double[][] testMark = new double[15][4];
String classAver = "";
for(int i=0; i < rowNumber; i++){
classAver += ((testMark[0][0] + testMark[0][1] + testMark[0][2] + testMark[0][3])
+
(testMark[1][0] + testMark[1][1] + testMark[1][2] + testMark[1][3]) +
(testMark[2][0] + testMark[2][1] + testMark[2][2] + testMark[2][3]) +
(testMark[3][0] + testMark[3][1] + testMark[3][2] + testMark[3][3]) +
(testMark[4][0] + testMark[4][1] + testMark[4][2] + testMark[4][3]) +
(testMark[5][0] + testMark[5][1] + testMark[5][2] + testMark[5][3]) +
(testMark[6][0] + testMark[6][1] + testMark[6][2] + testMark[6][3]) +
(testMark[7][0] + testMark[7][1] + testMark[7][2] + testMark[7][3]) +
(testMark[8][0] + testMark[8][1] + testMark[8][2] + testMark[8][3]) +
(testMark[9][0] + testMark[9][1] + testMark[9][2] + testMark[9][3]) +
(testMark[10][0] + testMark[10][1] + testMark[10][2] + testMark[10][3]) +
(testMark[11][0] + testMark[11][1] + testMark[11][2] + testMark[11][3]) +
(testMark[12][0] + testMark[12][1] + testMark[12][2] + testMark[12][3]) +
(testMark[13][0] + testMark[13][1] + testMark[13][2] + testMark[13][3]) +
(testMark[14][0] + testMark[14][1] + testMark[14][2] + testMark[14][3]));
}
outPut.setText(classAver); // <--How do I divide that number by 60?
答案 0 :(得分:5)
看到你指定的数组是double[15][4]
,你可以使用它:
BigDecimal i = BigDecimal.ZERO;
for (int row = 0; row < testMark.length; row++) {
for (int col = 0; col < testMark[row].length; col++) {
i = i.add(new BigDecimal(testMark[row][col]));
}
}
i = i.divide(new BigDecimal(60D));
outPut.setText(i.toString());
如果你想获得超过60个结果的平均值,那么在未来的路上:
BigDecimal i = BigDecimal.ZERO;
int count = 0;
for (int row = 0; row < testMark.length; row++) {
for (int col = 0; col < testMark[row].length; col++, count++) {
i = i.add(new BigDecimal(testMark[row][col]));
}
}
i = i.divide(new BigDecimal(count));
outPut.setText(i.toString());
Lordy,从哪里开始。
首先,循环:
for (int row = 0; row < 15; row++) { // these don't need to be concrete numbers ("15"), can be your array length
for (int col = 0; col < 4; col++) {
//append testMark[row][col]
}
}
其次,使用StringBuilder
,不要在循环中附加字符串
StringBuilder sb = new StringBuilder();
for (int row = 0; row < 15; row++) {
for (int col = 0; col < 4; col++) {
sb.append(testMark[row][col]); //can append delimiters, etc
}
}
String value = sb.toString(); //Your complete value
然后,施展你的价值:
BigInteger i = BigInteger.valueOf(classAver); //returns an int value
i = i.divide(new BigInteger(60));
outPut.setText(i.toString());
如果您不想将值添加为字符串,可以将它们添加为数字:
BigInteger i = BigInteger.ZERO;
for (int row = 0; row < 15; row++) {
for (int col = 0; col < 4; col++) {
try {
int raw = Integer.parseInt(testMark[row][col]);
i = i.add(new BigInteger(raw));
} catch (NumberFormatException ex) {
System.err.println(String.format("Bad value caught! (Row: %d, Col: %d)", row, col));
}
}
}
i = i.divide(new BigInteger(60));
outPut.setText(i.toString());
答案 1 :(得分:2)
根据您的更新,我认为您需要这样的内容 -
double[][] testMark = new double[15][4];
double total = 0; // start at 0.
double count = 0; // keep count.
for (int i = 0; i < testMark.length; i++) {
// iterate the inner array.
for (double d : testMark[i]) {
total += d; // add it to the total.
count++; // add one to the count.
}
}
String classAver = String.valueOf(total / count); // take average (as a String).
outPut.setText(classAver); // Set the text.
答案 2 :(得分:0)
classAver
不会保留您认为的价值。当你写
for(int i=0; i < rowNumber; i++){
classAver += [some long calculation that really needs to be done with loops];
}
等同于
for(int i=0; i < rowNumber; i++){
classAver = classAver + ...;
}
当您在+
上使用String
时,程序会将任何非String
个操作数转换为String
,并连接字符串。因此,如果您有5行并且您的计算产生1001,2001,3001,4001和5001,则生成的classAver
将不是"15005"
,而是"10012001300140015001"
。 (P.S.现在我知道它是double
,这会让事情变得更糟,因为你最终可能会出现像"1001.02001.03001.04001.05001.0"
这样的乱码。)
道德:当您进行计算时,请使用数字类型(int
,long
,double
,BigInteger
等。不要使用String
。完成计算后,如果您需要String
,然后转换它,例如:
double classAver;
[compute classAver]
outPut.setText(Double.toString(classAver)); // or Integer.toString(n) for integers
或
outPut.setText("" + classAver);