5 changed files with 44 additions and 0 deletions
@ -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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 118 KiB |
|
After Width: | Height: | Size: 63 KiB |
Loading…
Reference in new issue