定義一個表示學生信息的類Student,要求以下:java
(1)類Student的成員變量:ide
sNO 表示學號;this
sName表示姓名;spa
sSex表示性別;code
sAge表示年齡;對象
sJava:表示Java課程成績。blog
(2)類Student帶參數的構造方法:get
在構造方法中經過形參完成對成員變量的賦值操做。class
(3)類Student的方法成員:import
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<stus.length;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<stus.length;i++){ sum += stus[i].getsJava(); } System.out.println("平均成績:"+(sum/5)); } }