返回本章節
返回作業目錄
需求說明:
編寫JAVA程序,在控制檯輸入一位學生的英語考試成績,根據評測規則,輸出對應的成績等級。要求:定義方法實現學生成績的評測功能。
學生的英語考試成績進行評測,評測規則如下:
成績 >= 90:A
成績 >= 80:B
成績 >= 60:C
成績 < 60: D
實現思路:
定義方法getLevel,用於根據成績評測規則,輸出對應的成績等級。
根據需求說明要求,方法getLevel需要定義一個參數:int類型num,表示學生的英語成績,且需要返回評測結果,可通過返回值實現,因此方法getLevel的方法簽名如下:
public char getLevel(int num)
由於評測規則需要多次進行判斷,可使用多重if實現。
main方法中接收用戶輸入的學生英語成績,調用方法getLevel獲取評測結果並輸出。
實現代碼:
import java.util.Scanner;
public class EnglishLeveDemo {
public char getLevel(int score) {
char lecel;
if(score>=90){
lecel = 'A';
}else if(score>=80) {
lecel = 'B';
}else if(score>=60) {
lecel = 'C';
}else {
lecel = 'D';
}
return lecel;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入學生的英語成績:");
int score = sc.nextInt();
EnglishLeveDemo eld =new EnglishLeveDemo();
char result = eld.getLevel(score);
System.out.println("成績評測結果:"+result);
}
}