鍵盤錄入一個成績(整數(shù)類型),判斷并輸出成績的等級。

需求:鍵盤錄入一個成績(整數(shù)類型),判斷并輸出成績的等級。

要求使用兩種方式:if和switch

成績及對應等級如下

90~100? A

80~89??? B

70~79? C

60~69??? D

0~59????? E

If:

packagecom.briup.day5;


importjava.util.Scanner;


public class Test3{

?????? public static void main(String[] agrs) {

????????????? Scanner sc = new Scanner(System.in);

????????????? System.out.println("請輸入成績:");

????????????? int score = sc.nextInt();

????????????? if(90 <= score && score<= 100) {

???????????????????? System.out.println("A");

????????????? } else if(80 <= score&& score <= 89) {

???????????????????? System.out.println("B");

????????????? } else if(70 <= score&& score <= 79) {

???????????????????? System.out.println("C");

????????????? } else if(60 <= score&& score <= 69){

???????????????????? System.out.println("D");

????????????? } else if(score <= 59) {

???????????????????? System.out.println("E");

????????????? } else {

?????? System.out.println("成績錄入錯誤")

}

?????? }

}

Switch:

packagecom.briup.day5;????????????????????????????????????????????????????

?? import java.util.Scanner;

?? public class Test3 {

?????? public static void main(String[] args) {

?????????? Scanner sc = new Scanner(System.in);

?????????? System.out.println("請輸入成績:");

?????????? int score = sc.nextInt();


?????????? switch(score/10) {

????????????? case 10 :

????????????? 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;

??????????????????????????? case 5:

case 4:

case 3:

case 2:??

case 1:

case 0:

?????? System.out.println("E");

default:

?????? ???????????System.out.println("成績錄入錯誤");

?????????????break;

????????? }

????? }

? }?????

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容