zoukankan      html  css  js  c++  java
  • 学生成绩和平均成绩保存文件中的练习

    import java.io.*;
    import java.lang.*;
    import java.util.*;
    
    public class RecordScore {
    
    
        private String readline;
        private BufferedReader br;
        private BufferedWriter bw;
        private BufferedWriter bw2;
        private String [] str;
        private int score=0;
        private Stack<String> ta;
    
    
        static {
    
            System.out.println("输入成绩的格式为 姓名 成绩 ,例如:
     张三 80 
     李四 90
     王五 70
     请输入:
    ");
    
    
    
        }
    
    
        RecordScore() {
    
            br=new BufferedReader(new InputStreamReader(System.in));
            bw=new BufferedWriter(new OutputStreamWriter(System.out));
    
    
           try {
    
               ta=new Stack<String>();
               bw2=new BufferedWriter(new FileWriter(new File("D:\myRead\score.txt")));
    
    
           }catch (Exception e) {
    
               e.getMessage();
           }
    
        }
    
        private void setRecordScore() throws Exception {
    
            while ((readline=br.readLine())!=null) {
    
    
                if (readline.equals("end")) {
    
                    break;
    
                }
    
    
                str=readline.split(" "); //字符串按照空格拆分成数组
    
                 try  {
    
    
                     if(Integer.parseInt(str[1])<0||Integer.parseInt(str[1])>100) { // 输入的分数有异常的情况,超出分数范围的情况。
    
                         System.out.println("输入的分数有异常,请从新输入!");
    
                     }
    
                 }catch (ArrayIndexOutOfBoundsException e) {
    
                     e.getMessage();
                     System.out.println("没有空格");
                 }
    
    
                for(String s:str) {
    
                    ta.push(s);
    
                }
    
                score=score+Integer.parseInt(ta.pop());//累加分数,利用Stack后进先出的特性
    
                  bw.write(readline);
                  bw.newLine();
                  bw.flush();
                  bw2.write(readline);
                  bw2.newLine();
                  bw2.flush();
            }
    
            System.out.println("总分:"+score);
    
    
            bw.close();
            bw2.close();
        }
    
        public static void main(String[] args) throws Exception {
    
             RecordScore rs=new RecordScore();
    
             rs.setRecordScore();
    
        }
    }
  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    《EffectiveJava中文第二版》 高清PDF下载
    《MoreEffectiveC++中文版》 pdf 下载
    《啊哈c语言》 高清 PDF 下载
  • 原文地址:https://www.cnblogs.com/ltb6w/p/8278308.html
Copyright © 2011-2022 走看看