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

一个题目 完全成思路【新手】
任务一:已知某个班有M个学生,学习N门课程,已知所有学生的各科成绩,编程:分别求每个学生的平均成绩,并输出。
要求:
? 定义一个二维数组,用于存放M个学生的N门成绩。定义一个一维数组,用于存放每个学生的平均成绩。
? 做二重循环,将每个学生的成绩输入到该二维数组中。
? 做二重循环,对已经存在于二维数组的中的值进行平均分计算,将结果保存到一个一维数组中。
? 做循环输出该一维数组(即平均分)的值。


------解决方案--------------------
import java.util.ArrayList;
import java.util.List;

public class Test2 {

public static void main(String[] args) {

// 初始化数据
List<Student> sList = new ArrayList<Student>() {
{
add(new Student("学生一", new ArrayList<Kecheng>() {
{
add(new Kecheng("数学", 53));
add(new Kecheng("语文", 65));
add(new Kecheng("英语", 72));
}
}));
add(new Student("学生二", new ArrayList<Kecheng>() {
{
add(new Kecheng("数学", 12));
add(new Kecheng("语文", 78));
add(new Kecheng("英语", 93));
}
}));
add(new Student("学生二", new ArrayList<Kecheng>() {
{
add(new Kecheng("数学", 82));
add(new Kecheng("语文", 76));
add(new Kecheng("英语", 12));
}
}));
}
};
for (int i = 0; i < sList.size(); i++) {
// 三门分数总和
int sum = 0;
for (int j = 0; j < sList.get(i).kList.size(); j++) {
sum += sList.get(i).kList.get(j).score;
}
// 除出来的结果会有点偏差,可以使用BigDecimal
System.out.println(sList.get(i).name + "三门课程平均分:" + sum / 3f);
}
}
}

class Student {
String name;
List<Kecheng> kList;

public Student(String name, List<Kecheng> kList) {
super();
this.name = name;
this.kList = kList;
}

}

class Kecheng {
String name;
int score;

public Kecheng(String name, int score) {
super();
this.name = name;
this.score = score;
}

}



----------------------------------------------
这样写看起来很费劲....




------解决方案--------------------
Java code

package com.zf.test;
import java.util.*;
public class Test {
    
    int stuCount = 10;    //学生人数
    int curCount = 5;    //课程总数
    
    int[][] stuScore = new int[stuCount][curCount];    
    
    int [] stuAvgScore = new int[stuCount];
    
    Random random = new Random();
    
    //将学生分数放入二位数组
    public void putScoreToStuScore(){
        for (int i = 0; i < stuScore.length ; i++) {
            int tmp[] = new int[curCount];
            for (int j = 0; j < curCount ; j++) {
                tmp[j] = getRadomScore();
            }
            stuScore[i] = tmp;
        }
    }
    
    //初始化学生平均分数
    public void initAvgScore(){
        for (int i = 0; i < stuScore.length; i++) {
            int tmp[] = stuScore[i];
            int totalScore = 0 ;    //总分数
            for (int j = 0; j < tmp.length; j++) {
                totalScore += tmp[j];
            }
            stuAvgScore[i] = totalScore / tmp.length;    //得到平均分数
        }
    }
    
    //打印平均分数
    public void printAvgScore(){
        for (int i = 0; i < stuAvgScore.length; i++) {
            System.out.println("学生" + (i + 1) +"的平均分为:" + stuAvgScore[i]);
        }
    }
    
    //打印所有学生 所有课程信息
    public void printAllStudentScore(){
        for (int i = 0; i < stuScore.length; i++) {
            System.out.println("学生" + (i+1) + "的课程分数为:" + Arrays.toString(stuScore[i]));  
        }
    }
    
    //得到一个 60 - 100 的随机分数
    public int getRadomScore(){
        return random.nextInt(40) + 60;
    }
    
    public static void main(String[] args) {
        Test t  = new Test();
        t.putScoreToStuScore();        
        t.initAvgScore();
        t.printAllStudentScore();
        System.out.println("\n\n-------------------------------------------");
        t.printAvgScore();
    }

}