鍵盤讀入一個數字,判斷其為偶數或奇數:


import java.util.Scanner;

public class P525 {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        System.out.println("請輸入一個數字 ");
        int a = scn.nextInt();
        scn.close();

        // 如a能被2整除,為偶數,反之,奇數
        if (a % 2 == 0) {
            System.out.println(a + " 是偶數");
        } else {
            System.out.println(a + " 是奇數");
        }

    }
}


輸出結果:
請輸入一個數字:
7
7 是奇數

arrow
arrow

    ALVIN 發表在 痞客邦 留言(0) 人氣()