日期:2014-05-20  浏览次数:20790 次

询问二个简单的题目
1.从键盘输入20条记录:姓名、计算机成绩、英语成绩,每栏中间空两格,输出到my.txt

2.从my.txt输入,计算计算机平均成绩,然后将姓名、计算机成绩、英语成绩、计算机平均成绩输出到my2.txt里

请帮下忙。
谢谢了。

------解决方案--------------------
System.in
java.util.StringTokenizer

class Detail {
String name;
float sore1;
float sore2;
}

java.io.BufferedReader reader;

String line = reader.readLine();

StringTokenizer tokenizer = new StringTokenizer(line, "\\, ");

Detail user;

user.setName(tokenizer.nextElement());

....



------解决方案--------------------
算了,还是帮你做了一个,我把每个学生的平均成绩、所有学生的计算机平均成绩、英语平均成绩都保存到了 my2.txt 中,你参考一下吧,不一定写得很好。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
  public static void main(String[] args) throws IOException {
    inputScore();
    computeAverage();
  }
  
  public static void inputScore() throws IOException {
    Scanner sc = new Scanner(System.in);        
    System.out.print( "请输入需要输入的学生数量: ");
    int num = sc.nextInt();    
    BufferedWriter bw = new BufferedWriter(new FileWriter( "my.txt "));
    for(int i=0; i <num; i++){
      try{
        System.out.printf( "%n请输入第 %d 个字生的姓名: ", i+1);
        String name = sc.next();
        System.out.printf( "请输入 %s 的计算机成绩: ", name);
        int computer = sc.nextInt();
        System.out.printf( "请输入 %s 的英语成绩: ", name);
        int english = sc.nextInt();
        String info = name + " " + computer + " " + english + System.getProperty( "line.separator ");
        bw.write(info);
      }catch(InputMismatchException e){
        System.out.println( " ** 输入错误,请重新输入 ** ");
        sc.next();
        --i;
      }
    }
    System.out.println( "\n输入完成,已经存入到 my.txt 中 ");
    bw.close();
    sc.close();
  }
  
  public static void computeAverage() throws IOException {
    Scanner sc = new Scanner(new File( "my.txt "));
    List <String> list = new ArrayList <String> ();
    Pattern pattern = Pattern.compile( "( )(\\d+)( )(\\d+) ");
    int sum1 = 0;
    int sum2 = 0;
    int k = 0;
    while(sc.hasNextLine()){
      String tmp = sc.nextLine();      
      if(tmp.trim().length()> 0){
        Matcher matcher = pattern.matcher(tmp);
        matcher.find();
        int computer = Integer.parseInt(matcher.group(2));
        int english = Integer.parseInt(matcher.group(4));
        sum1 += computer;
        sum2 += english;
        float average = ((float)computer + english) / 2;
        k++;
        list.add(tmp + " " + String.format( "%.2f ", average));
      }
    }
    
    float average1 = ((float)sum1)/k;
    float average2 = ((float)sum2)/k;
    BufferedWriter bw = new BufferedWriter(new FileWriter( "my2.txt "));
    String line = System.getProperty( "line.separator ");
    bw.write( "姓名 计 英 平均 " + line);
    for(int i=0; i <list.size(); i++) {
      bw.write(list.get(i) + line);
    }
    bw.write( "================== " + line);