- 定义长度为2的二维数组,分别存入两个小组的学生成绩,第一组为3人,第二组为5人。
- 从键盘接收成绩给数组赋值,存入学生成绩。
- 设计一个实例方法方法calGroup(int rst[]), 计算并返回各组平均成绩。形参类型为一维数组(各小组)成 绩,返回值小组平均成绩,类型为double。
- 设计一个静态方法方法printResult(int rst[][]), 打印出各小组成绩明细。形参类型为整型二维数组
成 绩,无返回值。 - 调用静态方法printResult(int rst[][]),
打印出各小组成绩明细。 - 创建两个对象,分别以各小组成绩数
组为参数调用实例方法
calGroup(int rst[]),并打印出各小组平均
成绩。

package com.jdlh.common.utils;
import java.util.Scanner;
public class Demo {
private static int[][] arr = new int[2][];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
arr[0] = new int[3];
arr[1] = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("输入第"+(i+1)+"小组成绩:");
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = scanner.nextInt();
}
}
printResult(arr);
Demo demo = new Demo();
System.out.println("*****************");
double groupOne = demo.calGroup(arr[0]);
System.out.println("第一小组平均成绩:" + groupOne);
double groupTwo = demo.calGroup(arr[1]);
System.out.println("第二小组平均成绩:" + groupTwo);
}
public double calGroup(int rst[]){
if (rst.length == 0){
return 0.0;
}
double sum = 0.0;
for (int i = 0; i < rst.length; i++) {
sum += rst[i];
}
double res = sum/(double) rst.length;
return (double) Math.round(res * 100) / 100;
}
private static void printResult(int rst[][]){
System.out.println("*******************");
System.out.println("输出各小组成绩:");
for (int i = 0; i < rst.length; i++) {
System.out.println("第"+(i+1)+"小组:");
for (int j = 0; j < rst[i].length; j++) {
System.out.print(rst[i][j]+"\t");
}
System.out.println();
}
}
}
望采纳
public class GroupScores {
public static void main(String[] args) {
// 定义长度为2的二维数组,分别存入两个小组的学生成绩
int[][] scores = new int[2][];
// 第一组为3人
scores[0] = new int[3];
// 第二组为5人
scores[1] = new int[5];
// 从键盘接收成绩给数组赋值,存入学生成绩
// 第一组学生成绩
for (int i = 0; i < scores[0].length; i++) {
scores[0][i] = /*从键盘输入*/;
}
// 第二组学生成绩
for (int i = 0; i < scores[1].length; i++) {
scores[1][i] = /*从键盘输入*/;
}
// 调用静态方法printResult(int rst[][]),打印出各小组成绩明细
printResult(scores);
// 创建两个对象,分别以各小组成绩数组为参数调用实例方法calGroup(int rst[])
GroupScores group1 = new GroupScores();
GroupScores group2 = new GroupScores();
// 并打印出各小组平均成绩
System.out.println(group1.calGroup(scores[0]));
System.out.println(group2.calGroup(scores[1]));
}
// 设计一个实例方法方法calGroup(int rst[]),计算并返回各组平均成绩
public double calGroup(int[] scores) {
double sum = 0;
// 计算各组总成绩
for (int score : scores) {
sum += score;
}
// 计算并返回各组平均成绩
return sum / scores.length;
}
}