1. 程式人生 > >根據分數輸出對應的等級

根據分數輸出對應的等級

1.例項說明:

使用if語句,根據鍵盤錄入的分數,輸出對應的等級。

2.接受鍵盤資料錄入的步驟:

(1)建立一個掃描器物件;

(2)呼叫掃描器物件的nextInt()方法掃描資料;

(3)匯入包;

3.例項:

import java.util.*;
class Demo2{
	public static void main(String[] args) 
	{
	
		//建立一個掃描器
		Scanner scanner = new Scanner(System.in);
		//呼叫掃描器掃描鍵盤錄入的資料
		
		System.out.println("請輸入一個分數:");
		int score = scanner.nextInt(); //定義了一個num變數接收掃描到內容。
		
		if(score>=90&&score<=100){
			System.out.println("A等級");
		}else if(score>=80&&score<=89){

			System.out.println("B等級");
		}else if(score>=70&&score<=79){
			
			System.out.println("C等級");
		}else if(score>=60&&score<=69){
			
			System.out.println("D等級");
		}else if(score>=0&&score<=59){
			
			System.out.println("E等級");
		}else{
			System.out.println("補考..");
		}
	}
}

執行結果如下圖所示: