Browse Source

王烊烊202302050115W7

master
WangYangyang 2 months ago
parent
commit
5695a1923d
  1. BIN
      w7/2.png
  2. BIN
      w7/3.png
  3. BIN
      w7/4.png
  4. 15
      w7/Raw.java
  5. 36
      w7/ScoreCalculate.java
  6. 0
      w7/Scores.txt
  7. BIN
      w7/未重构.png
  8. BIN
      w7/重构后1.png

BIN
w7/2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

BIN
w7/3.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
w7/4.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

15
w7/Raw.java

@ -0,0 +1,15 @@
import java.io.*;
public class Raw {
public static void main(String[] args){
String filename="w7/Score.txt";
int sum=0;
int count=0;
BufferedReader br=new BufferedReader(new FileReader(filename));
String line;
while((line= br.readLine())!=null){
sum+=Integer.parseInt(line);
count++;
}
}
}

36
w7/ScoreCalculate.java

@ -0,0 +1,36 @@
import java.io.*;
public class ScoreCalculate {
public static void main(String[] args){
String filename="w7/Scores.txt";
int sum=0;
int count=0;
try(BufferedReader br=new BufferedReader(new FileReader(filename))){
String line;
while((line=br.readLine())!=null){
line=line.trim();
if(line.isEmpty()){
continue;
}
sum+=Integer.parseInt(line);
count++;
}
if(count>0){
double average=(double)sum/count;
System.out.println("平均分为"+average);
}
else{
System.out.println("无有效成绩");
}
}
catch(FileNotFoundException e){
System.out.println("文件未找到或不存在:"+e.getMessage());
}
catch(IOException e){
System.out.println("文件读取错误:"+e.getMessage());
}
catch(NumberFormatException e){
System.out.print("文件中包含无法解析为数字的内容:"+e.getMessage());
}
}
}

0
w7/Scores.txt

BIN
w7/未重构.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
w7/重构后1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Loading…
Cancel
Save