簡單學生資訊獲取及修改()
阿新 • • 發佈:2018-12-11
class ShowTotalMessage{ //顯示學生所有資訊 public static void showTotalMessage(Student student1) { System.out.println("該同學id: "+student1.getid()); System.out.println("該同學姓名:"+student1.getname()); System.out.println("該同學性別:"+student1.getsex()); System.out.println("該同學年齡:"+student1.getsage()); } } public class Student { private String id,name="張傑",sex="男";//分別是學號,姓名,性別。 private int age=20; //年齡 private int computerScore=0; //表示計算機課程的成績 private int englishScore=0; //表示英語課的成績 private int mathScore=0; //表示數學課的成績 private int totalCredit=0; //表示總學分, 90分及以上2學分,60及以上90以下1.5學分,不及格無學分 private int aveScore=0; //表示平均成績,平均成績等於已有成績的課程除以課程數,0分表示無此門課成績 /*獲得*/ String getid(){ //獲得當前物件的學號 return id; } String getname(){ //獲得當前物件的姓名 return name; } String getsex(){ //獲得當前物件的性別 return sex; } int getsage(){ //獲得當前物件的性別 return age; } int getComputerScore(){ //獲得當前物件的計算機課程成績 return computerScore; } int getMathsScore(){ //獲得當前物件的數學課程成績 return mathScore; } int getEnglishScore(){ //獲得當前物件的英語課程成績 return englishScore; } int getTotalCredit() { return totalCredit; } int getTveScore() { return aveScore; } /*設定*/ void setid(String id){ //設定當前物件的id值 this.id = id; } void setComputerScore(int computerScore){ //設定當前物件的computerScore值,在設定此值時修改總學分和平均成績 this.computerScore = computerScore; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } void setEnglishScore(int englishScore){ //設定當前物件的englishScore值,在設定此值時修改總成績和平均成績 this.englishScore=englishScore; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } void setMathScore(int mathScore){ //設定當前物件的mathScore值,在設定此值時修改總成績和平均成績 this.mathScore=mathScore; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } /*刪除*/ void delComputerScore(){ //刪除當前物件的computerScore值,在刪除此值時修改總學分和平均成績 this.computerScore = 0; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } void delEnglishScore(){ //刪除當前物件的englishScore值,在刪除此值時修改總成績和平均成績 this.englishScore = 0; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } void delMathScore(){ //刪除當前物件的mathScore值,在刪除此值時修改總成績和平均成績 this.mathScore = 0; this.totalCredit=computerScore+mathScore+englishScore; this.aveScore=(computerScore+mathScore+englishScore)/3; } public static void main(String []grgs){ //String []grgs不能刪,否則無法成功執行 Student student1 = new Student(); student1.setid("20178888"); student1.setComputerScore(66); student1.setEnglishScore(66); student1.setMathScore(66); System.out.print("hello world\n"); System.out.print("顯示該學生所有資訊:"); ShowTotalMessage.showTotalMessage(student1); System.out.print("該學生計算機成績:"); System.out.println(student1.getComputerScore()); System.out.print("該學生英語成績:"); System.out.println(student1.getEnglishScore()); System.out.print("該學生數學成績:"); System.out.println(student1.getEnglishScore()); System.out.println("總成績: "+student1.getTotalCredit()); System.out.println("平均成績: "+student1.getTveScore()); System.out.print("進行刪除動作:\n"); System.out.print("刪除計算機成績:\n"); student1.delComputerScore(); System.out.println("總成績: "+student1.getTotalCredit()); System.out.println("平均成績: "+student1.getTveScore()); System.out.print("刪除英語成績:\n"); student1.delEnglishScore(); System.out.println("總成績: "+student1.getTotalCredit()); System.out.println("平均成績: "+student1.getTveScore()); System.out.print("刪除數學成績:\n"); student1.delMathScore(); System.out.println("總成績: "+student1.getTotalCredit()); System.out.println("平均成績: "+student1.getTveScore()); } }