定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;
sName表示姓名;
sSex表示性别;
sAge表示年龄;
sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
方法一:
package com.Summer_0511.cn; import java.util.Arrays; class Student{ private int sNo; private String sName; private String sSex; private int sAge; private int sJava; public Student(int sNo, String sName, String sSex, int sAge, int sJava) { super(); this.sNo = sNo; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sJava = sJava; } public int getsNo() { return sNo; } public String getsName() { return sName; } public String getsSex() { return sSex; } public int getsAge() { return sAge; } public int getsJava() { return sJava; } @Override public String toString() { return "student [sNo=" + sNo + ", sName=" + sName + ", sSex=" + sSex + ", sAge=" + sAge + ", sJava=" + sJava + "]"; } } public class Test05 { public static void main(String[] args) { Student s1 = new Student(1, "小a","男" , 18, 95); Student s2 = new Student(2,"小b","女",20,90); Student s3 = new Student(3,"小c","女",21,65); Student s4 = new Student(4, "小d", "男", 18, 100); Student s5 = new Student(5, "小e", "男", 15, 85); int [] s = new int[5]; s[0] = s1.getsJava(); s[1] = s2.getsJava(); s[2] = s3.getsJava(); s[3] = s4.getsJava(); s[4] = s5.getsJava(); int sum = 0; int Max=s[1],Min=s[1]; for (int i = 0; i < s.length; i++) { sum = sum+s[i]; if (Max < s[i]) { Max = s[i]; } if (Min >s[i]) { Min = s[i]; } } System.out.println("java的平均成绩为"+sum/5); System.out.println("java成绩的最小值"+Min); System.out.println("java成绩的最大值"+Max); } }
方法二:
package stage3; class Student{ private int sNo; private String sName; private String sSex; private int sAge; private int sJava; public Student(int sNo, String sName, String sSex, int sAge, int sJava) { this.sNo = sNo; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sJava = sJava; } public int getsNo() { return sNo; } public void setsNo(int sNo) { this.sNo = sNo; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsSex() { return sSex; } public void setsSex(String sSex) { this.sSex = sSex; } public int getsAge() { return sAge; } public void setsAge(int sAge) { this.sAge = sAge; } public int getsJava() { return sJava; } public void setsJava(int sJava) { this.sJava = sJava; } } public class Test4 { public static void main(String[] args) { // 学员信息 Student stu1 = new Student(1, "郭靖", "男", 22, 90); Student stu2 = new Student(2, "欧阳锋", "男", 61, 91); Student stu3 = new Student(3, "杨康", "男", 20, 92); Student stu4 = new Student(4, "欧阳克", "男", 23, 93); Student stu5 = new Student(5, "穆念慈", "女", 24, 94); Student [] stus = new Student[5]; stus[0] = stu1; stus[1] = stu2; stus[2] = stu3; stus[3] = stu4; stus[4] = stu5; System.out.println("---信息---"); for(int i = 0; i){ System.out.println(stus[i].getsNo()+"\t" +stus[i].getsName() +"\t"+stus[i].getsSex() +"\t"+stus[i].getsAge() +"\t"+stus[i].getsJava()); } int sum = 0; for(int i = 0; i ){ sum += stus[i].getsJava(); } System.out.println("平均成绩:"+(sum/5)); } }