2012年4月4日 星期三

練習題 2_4:比較兩數大小

執行結果,如下。


程式碼,如下。

import java.util.Scanner;

public class EX2_4 {

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        System.out.print("請輸入正整數 A 》》》》》 ");
        int a = stdIn.nextInt();

        System.out.print("請輸入正整數 B 》》》》》 ");
        int b = stdIn.nextInt();

        String resultStr = "";
        if (a < 0 || b < 0) {
            resultStr = "數字不可以為負數。";
        } else {
            if (a > b) {
                resultStr = "A > B";

            } else if (a < b) {
                resultStr = "A < B";
            } else {
                resultStr = "A = B";
            }
        }

        System.out.println("\n" + resultStr);

    }

}

No Response to "練習題 2_4:比較兩數大小"

張貼留言