Penseur de marche 2020-04-11 10:34 采纳率: 50%
浏览 399
已采纳

请问各位大佬,做这个题目时,如果我想在Test类里面创建学生的对象时用到数组是怎么使用的呢?

(1)设计包含静态数据成员的Student类,在该类定义中包括:
数据成员:学号,年龄,分数score,及静态数据成员学生人数count;
定义成员函数用于设置和读取及显示学号,年龄,分数,累计学生人数;

        静态成员函数getCount( ) 用于返回总人数;

        外部函数average()用于求平均值。
(2)设计一个测试类Test在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的平均分。
    麻烦各位了,谢谢大家。
  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2020-04-11 15:39
    关注

    用数组的语法定义就可以了呀,或者直接用 List 列表。参考:

    import java.util.Scanner;
    
    public class Student {
        private String id;
        private int age;
        private double score;
        public Student(String id, int age, double score) {
            super();
            this.id = id;
            this.age = age;
            this.score = score;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public double getScore() {
            return score;
        }
        public void setScore(double score) {
            this.score = score;
        }
    
    
        public static void main(String[] args) {
            //定义数组
            Student [] students = new Student[10];
            Scanner in = new Scanner(System.in);
            for(int i=0;i<students.length;i++) {
                System.out.println("学号:");
                String id = in.nextLine();
                System.out.println("年龄:");
                int age = in.nextInt();
                System.out.println("分数:");
                double score = in.nextDouble();
                //数组元素赋值
                Student student = new Student(id,age,score);
                students[i] = student;
            }
    
            in.close();
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误