Browse Source

上传文件至 ''

main
peisishuang 2 months ago
parent
commit
c8dc85833f
  1. 44
      w7-成绩文件异常处理.txt
  2. BIN
      屏幕截图 2026-04-23 104842.png
  3. BIN
      屏幕截图 2026-04-23 104907.png
  4. BIN
      屏幕截图 2026-04-23 104919.png
  5. BIN
      屏幕截图 2026-04-23 104933.png

44
w7-成绩文件异常处理.txt

@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ScoreCalculator {
public static void main(String[] args) {
// 使用try-with-resources确保流自动关闭
try (BufferedReader reader = new BufferedReader(new FileReader("scores.txt"))) {
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
try {
// 处理每行数据
String[] parts = line.split(",");
if (parts.length < 2) {
throw new IllegalArgumentException("第" + lineNumber + "行数据格式错误: 缺少必要字段");
}
String studentId = parts[0].trim();
if (studentId.isEmpty()) {
throw new IllegalArgumentException("第" + lineNumber + "行数据格式错误: 学号为空");
}
try {
double score = Double.parseDouble(parts[1].trim());
System.out.println("学号: " + studentId + ", 成绩: " + score);
} catch (NumberFormatException e) {
System.err.println("第" + lineNumber + "行数据格式错误: 成绩不是有效数字 - " + e.getMessage());
}
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
} catch (FileNotFoundException e) {
System.err.println("文件不存在: " + e.getMessage());
} catch (IOException e) {
System.err.println("文件读取错误: " + e.getMessage());
} catch (Exception e) {
System.err.println("发生未知错误: " + e.getMessage());
}
}
}

BIN
屏幕截图 2026-04-23 104842.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

BIN
屏幕截图 2026-04-23 104907.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
屏幕截图 2026-04-23 104919.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

BIN
屏幕截图 2026-04-23 104933.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Loading…
Cancel
Save