A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© AntThinking_toT 高级黑马   /  2014-5-27 17:07  /  1155 人查看  /  9 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "scores "中。

9 个回复

倒序浏览
  1. import java.util.Map;
  2. import java.util.HashMap;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.BufferedWriter;
  6. import java.io.FileWriter;
  7. import java.io.IOException;

  8. public class ScoreStat
  9. {
  10.         public static void main(String[] args) throws IOException
  11.         {
  12.                 for(int i = 1; i <= 5; i++){
  13.                         System.out.println("Please input student "+i+" related infomation:");
  14.                         Map<String,String> stuInfo = getInput();
  15.                         double avg = avg(Double.parseDouble(stuInfo.get("Chinese")),Double.parseDouble(stuInfo.get("Math")),Double.parseDouble(stuInfo.get("English")));
  16.                         stuInfo.put("Avg",avg+"");
  17.                         if(save(stuInfo)){
  18.                                 System.out.println("OK");
  19.                         }
  20.                         else{
  21.                                 System.out.println("Error");
  22.                         }
  23.                 }
  24.         }
  25.         private static Map<String,String> getInput() throws IOException{
  26.                 Map<String,String> stu = new HashMap<String,String>();
  27.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  28.                 String buf = null;
  29.                 System.out.println("********************************************");
  30.                 System.out.print("Student No: ");
  31.                 buf = br.readLine();
  32.                 stu.put("stuNo",buf);

  33.                 System.out.print("Student name: ");
  34.                 buf = br.readLine();
  35.                 stu.put("Name",buf);
  36.                
  37.                 System.out.print("Score of Chinese: ");
  38.                 buf = br.readLine();
  39.                 stu.put("Chinese",buf);
  40.                
  41.                 System.out.print("Score of Math: ");
  42.                 buf = br.readLine();
  43.                 stu.put("Math",buf);

  44.                 System.out.print("Score of English: ");
  45.                 buf = br.readLine();
  46.                 stu.put("English",buf);
  47.                 return stu;
  48.         }
  49.         private static double avg(double score1,double score2,double score3){
  50.                 return (score1 + score2 + score3) / 3;
  51.         }
  52.         private static boolean save(Map<String,String> stuInfo) throws IOException{
  53.                 BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\scores.txt",true));
  54.                 bw.write("StuNo: "+stuInfo.get("stuNo"));
  55.                 bw.newLine();
  56.                 bw.write("Name: "+stuInfo.get("Name"));
  57.                 bw.newLine();
  58.                 bw.write("Chinese: "+stuInfo.get("Chinese"));
  59.                 bw.newLine();
  60.                 bw.write("Math: "+stuInfo.get("Math"));
  61.                 bw.newLine();
  62.                 bw.write("English: "+stuInfo.get("English"));
  63.                 bw.newLine();
  64.                 bw.write("Avg: "+stuInfo.get("Avg"));
  65.                 bw.newLine();
  66.                 bw.write("***********************");
  67.                 bw.newLine();
  68.                 bw.close();
  69.                 return true;
  70.         }
  71. }
复制代码

貌似木有做,是不是发的地方不对,浏览的人太少。
回复 使用道具 举报

什么好不好的?哎,哥们你报的是Android还是云?
回复 使用道具 举报
这个是我的入学测试题啊
回复 使用道具 举报
先看看题目,有时间写一下!
回复 使用道具 举报
支持一下。。。
回复 使用道具 举报
毕老师基础视频里面这个不是一个练习题吗
回复 使用道具 举报
明日辉煌 发表于 2014-5-28 07:14
先看看题目,有时间写一下!

千万不要有时间再写,那样一般是等于不写的,我每次都这样,必须要马上。
回复 使用道具 举报
AntThinking_toT 发表于 2014-5-28 15:46
千万不要有时间再写,那样一般是等于不写的,我每次都这样,必须要马上。 ...

现在正在看视频呢,看完然后翻回来再写,看自己的时间安排吧,没有那么绝对的!
回复 使用道具 举报
上班休息 来着瞄一眼。。 呵呵呵
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马