C 渣渣 2021-10-27 15:16 采纳率: 100%
浏览 54
已结题

Java求解 大神们 救救孩子吧

Java新建项目TR51,编写程序计算各小组学生的平均成绩
和总成绩,并计算所有学生的平均成绩。
1.定义长度为2的二维数组,分别存入两个小组的学生成绩,
第一组为3人,第二组为5人
2.从键盘接收成绩给数组赋值,存入学生成绩。

3.设计一个实例方法方法 calgroup( int rstl),计算并返回各组
平均成绩。形参类型为一维数组(各小纽)成绩,返回值小组平
均成绩,类型为 double。
4.设计一个静态方法方法 printresult( int rst),打印出各小
组成绩明细。形参类型为整型二维数组□
成绩,无返回值。
5.调用静态方法 printresult(int rstl0),(个一题用男可
打印出各小组成绩明细。
6.创建两个对象,分别以各小组成绩数组为参数调用实例方法
calgroup( nt rstd).并打印出各小组数组成绩

  • 写回答

2条回答 默认 最新

  • 兔子猫猫 2021-10-31 13:09
    关注

    一组对象

    /**
     * @author 嫣然
     */
    public class Group_one {
        private int[] grands_one;
    
        public int[] getGrands_one() {
            return grands_one;
        }
    
        public void setGrands_one(int[] grands_one) {
            this.grands_one = grands_one;
        }
    }
    

    二组对象

    /**
     * @author 嫣然
     */
    public class Group_two {
        private int[] grands_two;
    
        public int[] getGrands_two() {
            return grands_two;
        }
    
        public void setGrands_two(int[] grands_two) {
            this.grands_two = grands_two;
        }
    }
    

    主方法

    import java.util.Scanner;
    
    /**
     * @author 嫣然
     */
    public class Tr51 {
        int[][] grandsSum = new int[2][5];
        Group_one group_one=new Group_one();
        Group_two group_two=new Group_two();
        int[] one=new int[3];
        int[] two=new int[5];
        double sum_a=0;
        double sum_b=0;
        double avg_a=0;
        double avg_b=0;
        public void input(int x) {
            Scanner sc = new Scanner(System.in);
            if (x == 1) {
                System.out.println("请输入" + x + "组成绩:");
                for (int i = 0; i < 3; i++) {
                    grandsSum[0][i] = sc.nextInt();
                    one[i]=grandsSum[0][i];
                }
                group_one.setGrands_one(one);
                System.out.println("存入成功");
            }
            if (x == 2) {
                System.out.println("请输入" + x + "组成绩:");
                for (int i = 0; i < 5; i++) {
                    grandsSum[1][i] = sc.nextInt();
                    two[i]=grandsSum[1][i];
                }
                group_two.setGrands_two(two);
                System.out.println("存入成功");
            }
        }
    
        public double calgroup(int x) {
            if (x == 1) {
                for (int i = 0; i < grandsSum[0].length; i++) {
                    sum_a=grandsSum[0][i]+sum_a;
                }
                avg_a=sum_a/3;
                return avg_a;
            } else if (x == 2) {
                for (int i = 0; i < grandsSum[1].length; i++) {
                    sum_b=grandsSum[1][i]+sum_b;
                }
                avg_b=sum_b/5;
                return avg_b;
            } else {
                return 0;
            }
        }
        public static void printresult(int rst){
            Tr51 tr51 = new Tr51();
            tr51.input(rst);
            if (rst==1){
                System.out.println(+rst+"组平均成绩:"+tr51.calgroup(rst));
                tr51.sum_a=tr51.group_one.getGrands_one()[0]+tr51.group_one.getGrands_one()[1]+tr51.group_one.getGrands_one()[2];
                System.out.println("总成绩:"+tr51.sum_a);
                //成绩明细
                System.out.println("成绩明细:"+tr51.group_one.getGrands_one()[0]+" "+tr51.group_one.getGrands_one()[1]+" "+tr51.group_one.getGrands_one()[2]);
            }else if(rst==2){
                System.out.println(+rst+"组平均成绩:"+tr51.calgroup(rst));
                tr51.sum_b=tr51.group_two.getGrands_two()[0]+tr51.group_two.getGrands_two()[1]+tr51.group_two.getGrands_two()[2]+tr51.group_two.getGrands_two()[3]+tr51.group_two.getGrands_two()[4];
                System.out.println("总成绩:"+tr51.sum_b);
                //成绩明细
                System.out.println("成绩明细:"+tr51.group_two.getGrands_two()[0]+" "+tr51.group_two.getGrands_two()[1]+" "+tr51.group_two.getGrands_two()[2]+" "+tr51.group_two.getGrands_two()[3]+" "+tr51.group_two.getGrands_two()[4]);
            }else {
                System.out.println("输入有误");
            }
        }
    
        public static void main(String[] args) {
            System.out.println("请输入小组数据: 1.一组 2.二组");
            Scanner sc=new Scanner(System.in);
            int count= sc.nextInt();
            if (count==1){
                printresult(count);
            }else if (count==2){
                printresult(count);
            }else {
                System.out.println("输入有误");
            }
        }
    }
    
    运行结果:
    请输入小组数据: 1.一组 2.二组
    2
    请输入2组成绩:
    45
    56
    99
    77
    88
    存入成功
    2组平均成绩:73.0
    总成绩:365.0
    成绩明细:45 56 99 77 88
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月13日
  • 已采纳回答 11月5日
  • 创建了问题 10月27日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装