從鍵盤輸入一個成績分數,然後通過switch語句將其轉換爲對應等級
import java.util.Scanner;
public class TestScore //定義一個TestScore類
{
public static void main(String[] args) //主函數
{
while(true){ //使程序重複執行
Scanner sc=new Scanner(System.in); //創建並實例化對象sc
System.out.println("請輸入分數:");
int score=sc.nextInt();
if(score<0 || score>100) //使輸入的成績合法
{
System.out.println("請輸入正確的成績!");
}else{
switch(score/10) //通過執行switch語句得到對應等級
{
case 9:System.out.println("成績對應的等級爲:A"); break;
case 8:System.out.println("成績對應的等級爲:B"); break;
case 7:System.out.println("成績對應的等級爲:C"); break;
case 6:System.out.println("成績對應的等級爲:D"); break;
default:System.out.println("成績對應的等級爲:E"); break;
}
}
}
}
}