同学成绩

该博客展示了一段Java代码,实现了同学成绩的录入与读取功能。程序通过控制台输入同学的姓名、Java分数和数据结构分数,并将这些信息写入文件。之后,从文件中读取数据,计算每位同学的总分并输出。
 1 import java.beans.FeatureDescriptor;
 2 import java.io.*;
 3 import java.util.Scanner;
 4 
 5 
 6 public class num_3v {
 7     public static void main(String[] args) throws IOException {
 8         String[] sname = new String[5];
 9         int[] jScore = new int[5];
10         int[] sScore = new int[5];
11         File file = new File("e:\\data.txt");
12         if(! file.exists())
13             file.createNewFile();
14         Scanner sc = new Scanner(System.in);
15         FileWriter fw = new FileWriter(file);
16         BufferedWriter bw = new BufferedWriter(fw);
17         for (int i = 0; i < 3; i++) {
18             System.out.println("请输入" + (i + 1) + "同学的姓名:");
19             sname[i] = sc.next();
20             System.out.println("请输入" + (i + 1) + "同学的java分数:");
21             jScore[i] = sc.nextInt();
22             System.out.println("请输入" + (i + 1) + "同学的数据结构分数:");
23             sScore[i] = sc.nextInt();
24             bw.write(sname[i] + "," + jScore[i] + "," + sScore[i] + ",");
25             bw.newLine();
26         }
27         bw.close();
28         fw.close();
29         readDataFromFile(file);
30     }
31 
32     private static void readDataFromFile(File file) throws IOException {
33         FileReader fr = new FileReader(file);
34         BufferedReader br = new BufferedReader(fr);
35         String str = "";
36         int sum = 0;
37         while ((str = br.readLine()) != null) {
38             String[] stuInfo = str.split(",");
39             System.out.println("姓名" + stuInfo[0] + "Java分数:" + stuInfo + "数据结构分数:" + stuInfo[2]);
40             sum = Integer.parseInt(stuInfo[1]) + Integer.parseInt(stuInfo[2]);
41             System.out.println(stuInfo[0] + "的总分是:" + sum);
42         }
43         br.close();
44         fr.close();
45     }
46 }

 

转载于:https://www.cnblogs.com/WuYangdan-5201314/p/10954511.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值