設計一個學生類別(Student)。
類別具有姓名(name)、學號(id)、國文成績(chinese)、英文成績(english) 、數學成績(math)等欄位。
要設計有參數的建構子(包含姓名與學號),
然後要可以計算國英數三科平均。
內含有3位學生成績,計算其平均,並印出。
PS.可讓使用者輸入成績。檔名Score2.java
謝謝~~
2006-06-18 05:10:52 · 1 個解答 · 發問者 陵 1 in 電腦與網際網路 ➔ 程式設計
要讓使用者輸入?
可不可以幫我改一下^.^
2006-06-20 18:22:15 · update #1
public class Score2 { public static void main(String[] args) { Student a = new Student("A", "0001"); Student b = new Student("B", "0131"); Student c = new Student("C", "2311"); a.setChineseGrade(100); a.setEnglishGrade(91); a.setMathGrade(72); b.setChineseGrade(96); b.setEnglishGrade(44); b.setMathGrade(88); c.setChineseGrade(81); c.setEnglishGrade(99); c.setMathGrade(93); float cAvg = (a.getChineseGrade() + b.getChineseGrade() + c.getChineseGrade()) / 3.0F; float eAvg = (a.getEnglishGrade() + b.getEnglishGrade() + c.getEnglishGrade()) / 3.0F; float mAvg = (a.getMathGrade() + b.getMathGrade() + c.getMathGrade()) / 3.0F; System.out.println("國文總平均為: " + cAvg); System.out.println("英文總平均為: " + eAvg); System.out.println("數學總平均為: " + mAvg); float avg1 = (a.getChineseGrade() + a.getEnglishGrade() + a.getMathGrade()) / 3.0F; float avg2 = (b.getChineseGrade() + b.getEnglishGrade() + b.getMathGrade()) / 3.0F; float avg3 = (c.getChineseGrade() + c.getEnglishGrade() + c.getMathGrade()) / 3.0F; System.out.println("學生" + a.getName() + "的平均成績為: " + avg1); System.out.println("學生" + b.getName() + "的平均成績為: " + avg2); System.out.println("學生" + b.getName() + "的平均成績為: " + avg3); }}class Student { String name, id; int cGrade, eGrade, mGrade; Student(String n, String i) { name = n; id = i; } public void setName(String n) {name = n;} public void setID(String i) {id = i;} public void setChineseGrade(int c) {cGrade = c;} public void setEnglishGrade(int e) {eGrade = e;} public void setMathGrade(int m) {mGrade = m;} public String getName() {return name;} public String getID() {return id;} public int getChineseGrade() {return cGrade;} public int getEnglishGrade() {return eGrade;} public int getMathGrade() {return mGrade;}}
2006-06-20 05:54:37 · answer #1 · answered by ? 7 · 0⤊ 0⤋