假设有10个学生,每个数据包括姓名,学号,3门课的成绩,从键盘输入每个学生的数据,处理数据的输入异常,计算每个学生3门课的平均分,并分别计算3门课的所有学生的平均分。
我不懂的是如何把数据输入?求提点

解决方案 »

  1.   


    //数据输入,可以通过类型转换把用户输入的字符串转成相应的类型
    Scanner sc = new Scanner(System.in); //获取标准输入流扫描器
    String buf = sc.nextLine(); //获取用户输入的字符串
    try {
        int i = Integer.parseInt(buf); //把用户输入的字符串转成int类型
        double d = Double.parseDouble(buf);//把用户输入的字符串转成double类型
    } catch (Exception e) {
        //异常处理
    }
    //for example
    class Student {
        String name;
        String id;
        double[] scores = new double[3];    public Student() {}
        public Student(String name, String id) {
            this.name = name;
            this.id = id;
        }    public void setName(String name) {this.name=name;}
        public void setId(String id) {this.id=id;}
        public void setScore(double[] scores) {
            for (int i=0; i<Math.min(this.scores.length, scores.length); i++) {
                this.scores[i] = scores[i];
            }
        }
        public void setScore(int index, double score) {
            if (index<0||index>=this.scores.length) return;
            this.scores[index] = score;
        }
        public String getName(){return name;}
        public String getId() {return id;}
        public double[] getScores() {return Arryas.copyOf(scores, scores.length);}
        public double getScore(int index) {
            if (index<0||index>=this.scores.length) return -1;
            return this.scores[index];
        }
        public String toString() {
            return String.format("name=%s, id=%s, score1=%.2f, score2=%.2f, score3=%.2f", 
                                  name, id, scores[0], scores[1], scores[2]);
        }
        public double getSum() {
            double sum = 0;
            for (int i=0; i<scores.length; i++) {
                sum += scores[i];
            }
            return sum;
        }
        public double getAvg() {
            return getSum()/scores.length;
        }
    }public class Test {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int cnt = 0;
            Student[] stu = new Student[10];
            String name, id;
            double[] scores = new double[3];
            while (cnt < stu.length) {
                System.out.println("please input student name");
                name = sc.nextLine();
                System.out.println("please input student id");
                id = sc.nextLine();
                for (int i=0; i<scores.length;) {
                    try {
                        System.out.printf("please input student score[%d]\n", i+1);
                        scores[i] = Double.parseDouble(sc.nextLine());
                        i++;
                    } catch (Exception e) {
                        System.out.println("error input, please input again");
                    }  
                } 
                stu[cnt] = new Student(name, id);
                stu[cnt].setScores(scores);
                cnt++;     
            }
            double sum = 0;
            for (int i=0; i<stu.length; i++) {
                System.out.printf("name=%s, avg=%.2f\n", stu[i].getName(), stu[i].getAvg());        sum += stu.getSum();
            }
            System.out.printf("total avg: %.2f\n", sum/stu.length/scores.length);
        }
    }
      

  2.   


    public static void main(String[] args) {
    System.out.println("输入语文成绩");
    String core = getInput();
    System.out.println("语文成绩是: " + core);
    }public static String getInput() {
    InputStreamReader readerResponse = new InputStreamReader(System.in);
    BufferedReader bufferResponse = new BufferedReader(readerResponse);
    try {
    return bufferResponse.readLine();
    } catch (IOException e) {
    return null;

    }
    }其他的LZ自己做一下吧