编写一个程序:要求从idea控制台以提问对话的形式,输入三个同学的姓名、年龄、c语言成绩与java成绩,并将数据保存到一个“chengji.txt”的文件中.
时间: 2023-07-16 07:16:04 浏览: 92
好的,以下是程序代码:
```java
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
FileWriter writer = new FileWriter("chengji.txt");
for (int i = 1; i <= 3; i++) {
System.out.println("请输入第" + i + "个同学的姓名:");
String name = scanner.next();
System.out.println("请输入第" + i + "个同学的年龄:");
int age = scanner.nextInt();
System.out.println("请输入第" + i + "个同学的C语言成绩:");
int cScore = scanner.nextInt();
System.out.println("请输入第" + i + "个同学的Java成绩:");
int javaScore = scanner.nextInt();
writer.write(name + " " + age + " " + cScore + " " + javaScore + "\n");
}
writer.close();
System.out.println("数据已保存到chengji.txt文件中。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
运行程序后,控制台会提示输入每个同学的姓名、年龄、C语言成绩和Java成绩,输入完毕后,数据会保存到当前目录下的chengji.txt文件中。
阅读全文