gpt4 book ai didi

java - 我的成绩簿程序无法正确打印到 Excel 文档

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:00 25 4
gpt4 key购买 nike

我的成绩簿程序(我们输入成绩并查找平均值)在打印到 Excel 文档时无法正常工作。只有学生 1 和标题打印中的平均数以及学生 1 的平均数下方为 0,没有测试成绩或任何内容。

这是我的代码:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class array {


public static final String Dabook = "wording.xls";



@SuppressWarnings("unused")
public static void main(String[] args) throws IOException {

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("wording.xls"));
HSSFSheet sheet = wb.getSheet("Sheet1");

int col = 0, num = 1;
Row startRow = sheet.createRow(0);
Cell startCell = startRow.createCell(0);
int cellNumber = 0;
double total = 0;

double [ ] [ ] gridGrades =
{ { 87.5, 61.0, 70.5 },
{ 90.0, 88.5, 87.5 },
{ 78.0, 65.5, 70.5 },
{ 80.0, 79.0, 81.5 },
{ 60.5, 55.5, 50.0 } };
for (int colTest = 0; colTest < gridGrades[colTest].length + 1; colTest++) {
for (int testNum = 0; testNum < gridGrades[colTest].length + 1;testNum++) {

if (testNum == 1) {
startCell.setCellValue("");
}
if (testNum == gridGrades[0].length) {
startCell = startRow.createCell(testNum + 1);
startCell.setCellValue("Average");
startCell = startRow.createCell(testNum + 2);

}
}
}
for (double grade[] : gridGrades) {
startRow = sheet.createRow(num);
startCell = startRow.createCell(cellNumber);
startCell.setCellValue("Student " + num);

for (int poop = 0; poop < gridGrades[num - 1].length; poop++) {
if (poop == gridGrades[0].length - 1) {
double average = total / gridGrades[num - 1].length;

startCell = startRow.createCell(poop + 2);
DecimalFormat df = new DecimalFormat("###.##");
Double averageDouble = Double.parseDouble(df
.format(average));
startCell.setCellValue(averageDouble);
startCell = startRow.createCell(poop + 3);

total = 0;
}
for (int Scott = 0; startRow.getCell(Scott) != null; Scott++) {
sheet.autoSizeColumn(Scott);
}

FileOutputStream output = new FileOutputStream("wording.xls");
wb.write(output);
output.close();
}

}
{
System.out.printf(" Test %d: Test %d: Test %d: Average:%n", num, num+1, num+2);
for(double[] grade : gridGrades) {
double average = (grade[col] + grade[col+1] + grade[col+2])/3;
System.out.printf("Student %d %.2f %.2f %.2f %.2f %n", num, grade[col], grade[col+1], grade[col+2], average);
num++;


}
}

}}

我做错了什么?

最佳答案

  • 将写入 block 放在循环之外,只需写入一次。
  • 正如 @user902383 所说,您需要在外部循环中使用 num++ 语句。
  • 您从未在单元格中设置成绩,也从未在标题“测试 N”中设置成绩。
  • 当您在单元格中设置成绩(如 @user902383)时,请将此值添加到总计中。

希望这有帮助。

关于java - 我的成绩簿程序无法正确打印到 Excel 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22353657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com