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("输入成绩的格式为 姓名 成绩 ,例如:\n 张三 80 \n 李四 90\n 王五 70\n 请输入:\n"); } 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(); } }