日期:2014-05-20 浏览次数:20978 次
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();
}
}