要求使用JAVA编程,输入学生信息(学号,姓名,籍贯,课程名称),输入完一个学生信息后,会提示是否再输入下一个学生,如果不输入,键入N退出。将学生信息存入文档score.txt中,然后输入查询课程的名称,从score.txt中将选修了该门课程的学生信息(姓名 课程名称,成绩)读取出来,按照成绩从小到大的顺序排序,并在控制台输出,格式为如下图所示
时间: 2024-03-17 14:39:44 浏览: 17
好的,我会用JAVA编程来实现你的需求。
首先,我们需要定义一个学生类,用来存储每个学生的信息:
```java
public class Student {
private String id;
private String name;
private String hometown;
private String course;
private int score;
public Student(String id, String name, String hometown, String course, int score) {
this.id = id;
this.name = name;
this.hometown = hometown;
this.course = course;
this.score = score;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getHometown() {
return hometown;
}
public String getCourse() {
return course;
}
public int getScore() {
return score;
}
}
```
接下来,我们需要实现一个方法来让用户输入学生信息,并将学生信息存入文档score.txt中:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static final String FILE_NAME = "score.txt";
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入学生信息(学号 姓名 籍贯 课程名称):");
String line = scanner.nextLine();
String[] parts = line.split(" ");
if (parts.length != 4) {
System.out.println("输入格式不正确,请重新输入!");
continue;
}
String id = parts[0];
String name = parts[1];
String hometown = parts[2];
String course = parts[3];
System.out.print("是否继续输入(Y/N)?");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("N")) {
break;
}
students.add(new Student(id, name, hometown, course, 0));
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME));
for (Student student : students) {
writer.write(student.getId() + " " + student.getName() + " " + student.getHometown() + " " + student.getCourse() + " " + student.getScore());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们使用了一个ArrayList来存储所有的学生信息。当用户输入完一个学生信息后,我们会询问用户是否继续输入,如果用户输入N,则退出输入循环。
接下来,我们将学生信息写入文档score.txt中。这里我们使用了Java的文件写入操作,将每个学生信息写入一行,并使用空格作为分隔符。
最后,我们需要实现一个方法来查询选修了某门课程的学生信息,并按照成绩从小到大的顺序排序并输出:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
private static final String FILE_NAME = "score.txt";
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要查询的课程名称:");
String course = scanner.nextLine();
try {
BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
if (parts.length != 5) {
continue;
}
String id = parts[0];
String name = parts[1];
String hometown = parts[2];
String courseName = parts[3];
int score = Integer.parseInt(parts[4]);
if (courseName.equals(course)) {
students.add(new Student(id, name, hometown, courseName, score));
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
if (students.isEmpty()) {
System.out.println("没有选修该课程的学生!");
} else {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getScore() - s2.getScore();
}
});
System.out.println("姓名\t课程名称\t成绩");
for (Student student : students) {
System.out.println(student.getName() + "\t" + student.getCourse() + "\t" + student.getScore());
}
}
System.out.print("是否继续查询(Y/N)?");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("N")) {
break;
}
students.clear();
}
}
}
```
在上面的代码中,我们首先询问用户要查询的课程名称,并从文档score.txt中读取所有选修了该课程的学生信息,并存储在一个ArrayList中。
然后,我们判断ArrayList是否为空,如果为空则表示没有选修该门课程的学生,否则我们按照成绩从小到大的顺序排序,并在控制台输出每个学生的姓名、课程名称和成绩。
最后,我们会询问用户是否继续查询,如果用户输入N,则退出查询循环,并清空ArrayList。