编写一个程序:要求从idea控制台以提问对话的形式,输入三个同学的姓名、年龄、c语言成绩与java成绩,并将数据保存到一个“chengji.txt”的文件中. 提示: 创建一个Student学生类,其中域变量包括name,age,javaScore和cScore; 创建一个TestChengji类,在main主方法中创建一个学生对象数组, StudentInfo=new Student[3] 将控制台输入的数据保存在数组中,然后再将数组中的成绩到一个文件中。使用带缓冲的数据流
时间: 2023-07-16 13:17:30 浏览: 82
利用java实现学生成绩
好的,以下是代码实现:
**Student.java**
```java
public class Student {
private String name;
private int age;
private double javaScore;
private double cScore;
public Student(String name, int age, double javaScore, double cScore) {
this.name = name;
this.age = age;
this.javaScore = javaScore;
this.cScore = cScore;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getJavaScore() {
return javaScore;
}
public double getcScore() {
return cScore;
}
}
```
**TestChengji.java**
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TestChengji {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[3];
for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的姓名、年龄、C语言成绩和Java成绩:");
String name = scanner.next();
int age = scanner.nextInt();
double cScore = scanner.nextDouble();
double javaScore = scanner.nextDouble();
students[i] = new Student(name, age, javaScore, cScore);
}
BufferedWriter writer = new BufferedWriter(new FileWriter("chengji.txt"));
for (int i = 0; i < 3; i++) {
writer.write("姓名:" + students[i].getName() + ",年龄:" + students[i].getAge() + ",C语言成绩:" + students[i].getcScore() + ",Java成绩:" + students[i].getJavaScore() + "\n");
}
writer.close();
}
}
```
运行程序后,按照提示依次输入三个学生的姓名、年龄、C语言成绩和Java成绩,然后程序会将数据保存在名为“chengji.txt”的文件中。
阅读全文