补全代码:nalysisResult.javaimport java.io.*;import java.util.*;public class AnalysisResult { public static void main(String args[]) { //使用循环按行读取文件 //调用Fenxi.getTotalScore()获取解析出的总成绩,并写入scoreAnalysis.txt中 }}Fenxi.javaimport java.util.*;public class Fenxi { public static double getTotalScore(String s) { String regex="[^0123456789]" ; //匹配非数字的正则表达式 String digitMess=s.replaceAll(regex,"*"); StringTokenizer fenxi = new StringTokenizer(digitMess,"*"); double totalScore=0; while(fenxi.hasMoreTokens()){ double score = Double.parseDouble(fenxi.nextToken()); //累加 totalScore } return totalScore; }}
时间: 2023-06-14 16:05:58 浏览: 120
Java认证:Java中import使用.doc
import java.io.*;
import java.util.*;
public class AnalysisResult {
public static void main(String args[]) {
try {
File file = new File("score.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
double totalScore = 0;
while ((line = bufferedReader.readLine()) != null) {
totalScore += Fenxi.getTotalScore(line);
}
bufferedReader.close();
FileWriter fileWriter = new FileWriter("scoreAnalysis.txt");
fileWriter.write("总成绩:" + totalScore);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.util.*;
public class Fenxi {
public static double getTotalScore(String s) {
String regex = "[^0123456789]";
String digitMess = s.replaceAll(regex, "*");
StringTokenizer fenxi = new StringTokenizer(digitMess, "*");
double totalScore = 0;
while (fenxi.hasMoreTokens()) {
double score = Double.parseDouble(fenxi.nextToken());
totalScore += score;
}
return totalScore;
}
}
阅读全文