如何用java读取一个文件中的数字
例如:score.txt李寻欢:89,67,80,56刘备:75,83,25乔丹:68,99小甜甜布莱尼:78,23,100读取这个文件,计算每个学生的平均分从高到底排序...
例如:
score.txt
李寻欢:89,67,80,56
刘备:75,83,25
乔丹:68,99
小甜甜布莱尼:78,23,100
读取这个文件,计算每个学生的平均分
从高到底排序后
输出到另外一个文件
1.乔丹:83.5
2.李寻欢:73
3.小甜甜布莱尼:67
4.刘备:61 展开
score.txt
李寻欢:89,67,80,56
刘备:75,83,25
乔丹:68,99
小甜甜布莱尼:78,23,100
读取这个文件,计算每个学生的平均分
从高到底排序后
输出到另外一个文件
1.乔丹:83.5
2.李寻欢:73
3.小甜甜布莱尼:67
4.刘备:61 展开
展开全部
代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
String file = "J:\\score.txt";
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
List als = new ArrayList();
while (br.ready()) {
Score score = new Score();
String line = br.readLine();
String[] str = line.split(":");
score.setName(str[0]);
String[] sc = str[1].split(",");
for (int i = 0; i < sc.length; i++) {
score.setAllScore(score.getAllScore()
+ Double.valueOf(sc[i]).doubleValue());
}
score.setScore(score.getAllScore() / sc.length);
als.add(score);
}
br.close();
fr.close();
for (int i = 0; i < als.size() - 1; i++) {
Score max = (Score) als.get(i);
for (int j = i + 1; j < als.size(); j++) {
Score tmp = (Score) als.get(j);
if (max.getScore() < tmp.getScore()) {
max = tmp;
als.remove(j);
als.add(i, max);
}
}
}
System.out.println("姓名\t平均分");
for (int i = 0; i < als.size(); i++) {
Score tmp = (Score) als.get(i);
System.out.println(tmp.getName() + "\t" + tmp.getScore());
}
} catch (Exception e) {
}
}
}
class Score {
private String name = "";
private double allScore = 0.0;
private double score = 0.0;
public double getAllScore() {
return allScore;
}
public void setAllScore(double allScore) {
this.allScore = allScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
输入出结果
姓名 平均分
乔丹 83.5
李寻欢 73.0
小甜甜布莱尼 67.0
刘备 61.0
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
String file = "J:\\score.txt";
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
List als = new ArrayList();
while (br.ready()) {
Score score = new Score();
String line = br.readLine();
String[] str = line.split(":");
score.setName(str[0]);
String[] sc = str[1].split(",");
for (int i = 0; i < sc.length; i++) {
score.setAllScore(score.getAllScore()
+ Double.valueOf(sc[i]).doubleValue());
}
score.setScore(score.getAllScore() / sc.length);
als.add(score);
}
br.close();
fr.close();
for (int i = 0; i < als.size() - 1; i++) {
Score max = (Score) als.get(i);
for (int j = i + 1; j < als.size(); j++) {
Score tmp = (Score) als.get(j);
if (max.getScore() < tmp.getScore()) {
max = tmp;
als.remove(j);
als.add(i, max);
}
}
}
System.out.println("姓名\t平均分");
for (int i = 0; i < als.size(); i++) {
Score tmp = (Score) als.get(i);
System.out.println(tmp.getName() + "\t" + tmp.getScore());
}
} catch (Exception e) {
}
}
}
class Score {
private String name = "";
private double allScore = 0.0;
private double score = 0.0;
public double getAllScore() {
return allScore;
}
public void setAllScore(double allScore) {
this.allScore = allScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
输入出结果
姓名 平均分
乔丹 83.5
李寻欢 73.0
小甜甜布莱尼 67.0
刘备 61.0
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询