欢迎光临散文网 会员登陆 & 注册

Java oop的题与答案5:统计一下班级所有学生的总成绩,静态,数组对象,构造方法

2019-12-14 15:10 作者:诗书画唱  | 我要投稿

//统计一下班级所有学生的总成绩

//1.学生类静态属性总成绩,成员属性姓名,成绩

//2.在构造方法里添加姓名和成绩,构造方法中每次添加总成绩

//3.打印成总成绩

//4.将每个学生保存到数组对象里,统计一共有多少个学生,打印他们的姓名成绩

//5.打印班级的平均成绩

package a;

public class student //public可不写,不写就有默认的访问修饰符

{

public   static   void    main(String[ ] args) {

student[] stdAry = new student[3] ;

stdAry[0] =new student("张三",80);

stdAry[1] =new student("李 四",70) ;

stdAry[2] =new student("王 五",60) ;

for (int i = 0; i < stdAry . length; i++){


renshu++;


}

System . out . println("总人数为"+renshu) ;

System . out . println("总成绩为"+student.zongchengji) ;

System . out . println("平均成绩为"+student.zongchengji/renshu) ;


}



public static int zongchengji;//静态属性总成绩,还可以这样写:public static int zongchengji=0;

//不写=的部分就是默认zongchengji为0,为了直观还是写吧。

public int chengji;//成员属性成绩

public  static int renshu=0;

public String name ;//姓名


//在构造方法里对姓名和成绩进行赋值

public student(String name, int chengji){

zongchengji=zongchengji+chengji;


this. name=name;

this . chengji=chengji;

     

System . out . println("姓名为"+name) ;



}



}

运行效果




Java oop的题与答案5:统计一下班级所有学生的总成绩,静态,数组对象,构造方法的评论 (共 条)

分享到微博请遵守国家法律