java中键盘录入三个同学的姓名、语文成绩、数学成绩、英语成绩,然后按照成绩从高到低写入文本,最后从文件中把数据读取出来显示在控制台上
时间: 2024-05-01 09:20:12 浏览: 114
可以按照以下步骤实现:
1. 创建一个Student类,包含姓名和三门课成绩的属性,以及按照成绩排序的方法。
```
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int chineseScore, int mathScore, int englishScore) {
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public String getName() {
return name;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public void sortScore() {
int[] scores = {chineseScore, mathScore, englishScore};
Arrays.sort(scores);
chineseScore = scores[2];
mathScore = scores[1];
englishScore = scores[0];
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chineseScore=" + chineseScore +
", mathScore=" + mathScore +
", englishScore=" + englishScore +
'}';
}
}
```
2. 在主方法中,通过Scanner类从键盘输入三个学生的信息,并创建Student对象。
```
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
System.out.println("请输入第" + i + "个学生的姓名、语文成绩、数学成绩、英语成绩:");
String name = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int englishScore = scanner.nextInt();
Student student = new Student(name, chineseScore, mathScore, englishScore);
student.sortScore(); // 按成绩排序
students.add(student);
}
}
```
3. 将学生信息按照成绩从高到低写入文本文件。
```
File file = new File("students.txt");
if (!file.exists()) {
file.createNewFile();
}
PrintWriter writer = new PrintWriter(new FileWriter(file));
for (Student student : students) {
writer.println(student.getName() + " " + student.getChineseScore() + " " + student.getMathScore() + " " + student.getEnglishScore());
}
writer.close();
```
4. 从文件中读取数据,并显示在控制台上。
```
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(" ");
String name = parts[0];
int chineseScore = Integer.parseInt(parts[1]);
int mathScore = Integer.parseInt(parts[2]);
int englishScore = Integer.parseInt(parts[3]);
Student student = new Student(name, chineseScore, mathScore, englishScore);
System.out.println(student);
}
reader.close();
```
完整代码如下:
```
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
System.out.println("请输入第" + i + "个学生的姓名、语文成绩、数学成绩、英语成绩:");
String name = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int englishScore = scanner.nextInt();
Student student = new Student(name, chineseScore, mathScore, englishScore);
student.sortScore(); // 按成绩排序
students.add(student);
}
File file = new File("students.txt");
if (!file.exists()) {
file.createNewFile();
}
PrintWriter writer = new PrintWriter(new FileWriter(file));
for (Student student : students) {
writer.println(student.getName() + " " + student.getChineseScore() + " " + student.getMathScore() + " " + student.getEnglishScore());
}
writer.close();
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(" ");
String name = parts[0];
int chineseScore = Integer.parseInt(parts[1]);
int mathScore = Integer.parseInt(parts[2]);
int englishScore = Integer.parseInt(parts[3]);
Student student = new Student(name, chineseScore, mathScore, englishScore);
System.out.println(student);
}
reader.close();
}
}
class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int chineseScore, int mathScore, int englishScore) {
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public String getName() {
return name;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public void sortScore() {
int[] scores = {chineseScore, mathScore, englishScore};
Arrays.sort(scores);
chineseScore = scores[2];
mathScore = scores[1];
englishScore = scores[0];
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chineseScore=" + chineseScore +
", mathScore=" + mathScore +
", englishScore=" + englishScore +
'}';
}
}
```
阅读全文