2012年4月4日 星期三

練習題 2_6:計算薪水

計算員工薪水,底薪 : 25000,100000 以上 獎金 5%,300000 以上 獎金 7%,500000 以上 獎金 10%。

執行結果,如下。


程式碼,如下。

import java.util.Scanner;

public class EX2_6 {

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

        System.out.print("\n請輸入業績 》》》》》 ");
        double accomplishment = stdIn.nextDouble();

        double salary = 25000;
        double prize = 0;

        if (accomplishment >= 100000 && accomplishment < 300000) {
            prize = accomplishment * 0.05;
        } else if (accomplishment >= 300000 && accomplishment < 500000) {
            prize = accomplishment * 0.07;
        } else if (accomplishment >= 500000) {
            prize = accomplishment * 0.1;
        }

        System.out.println("本月薪資 : " + (salary + prize) + "(底薪:" + salary
                + " 獎金:" + prize + ")");

    }

}

練習題 2_5:月份英翻中

執行結果,如下。


程式碼,如下。

import java.util.Scanner;

public class EX2_5 {

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

        System.out.print(" 請輸入月份 》》》》》 ");
        String month = stdIn.next();

        String outStr = "輸入錯誤。";

        if ("1".equals(month)) {
            outStr = "Januery";
        } else if ("2".equals(month)) {
            outStr = "February";
        } else if ("3".equals(month)) {
            outStr = "March";
        } else if ("4".equals(month)) {
            outStr = "April";
        } else if ("5".equals(month)) {
            outStr = "May";
        } else if ("6".equals(month)) {
            outStr = "June";
        } else if ("7".equals(month)) {
            outStr = "July";
        } else if ("8".equals(month)) {
            outStr = "August";
        } else if ("9".equals(month)) {
            outStr = "Stptember";
        } else if ("10".equals(month)) {
            outStr = "October";
        } else if ("11".equals(month)) {
            outStr = "November";
        } else if ("12".equals(month)) {
            outStr = "December";
        }

        System.out.println(" 英文 》》》》》 " + outStr);
    }

}

練習題 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);

    }

}

學習範例 2_7:判斷成績(2)

成績 100~90: A, 80~89:B, 70~79:C, 60~69:D, 60以下,不及格。

程式碼,如下。

import java.util.Scanner;
class P2_7{
    public static void main(String[] args){
        Scanner stdIn = new Scanner(System.in);
       
        System.out.print("請輸入成績 : ");
        int studentGrade = stdIn.nextInt();
       
        if(studentGrade > 100 || studentGrade < 0){
            System.out.println("輸入錯誤");
        }else if(studentGrade >= 90 && studentGrade <= 100){
            System.out.println("A");
        }else if(studentGrade >= 80 && studentGrade <= 89){
            System.out.println("B");
        }else if(studentGrade >= 70 && studentGrade <= 79){
            System.out.println("C");
        }else if(studentGrade >= 60 && studentGrade <= 69){
            System.out.println("D");
        }else {
            System.out.println("不及格。");
                }
    }
}

執行結果,如下。

2012年3月31日 星期六

練習題 2_3 : 判斷是否能整除

由鍵盤輸入 A、B兩數,判斷 A 是否能被B整除。

執行結果,如下。


程式碼,如下。

import java.util.Scanner;

public class EX2_3 {

    public static void main(String[] args) {
        Scanner std = new Scanner(System.in);
       
        System.out.println("請輸入任意兩個數字");
       
        System.out.print("數字 A 》》》》》 ");
        double x = std.nextDouble();
        System.out.print("數字 B 》》》》》 ");
        double y = std.nextDouble();
       
        if(x%y == 0){
            double times = x/y;
            System.out.println("A是B的" + times + " 倍。" );
        }else{
            System.out.println("A不可以被B整除。");
        }

    }
}

2012年3月28日 星期三

練習題 2_2:判斷兩數差距

判斷兩數是否差距 5.5 以上,使用 Math.abs 方法,取絕對值。

執行結果,如下 :



程式碼。如下 :

import java.util.Scanner;

public class EX2_2 {

    public static void main(String[] args) {
        Scanner std = new Scanner(System.in);
       
        System.out.print("請輸入數字一 》》》》》 ");
        double number1 = std.nextDouble();
       
        System.out.print("請輸入數字二 》》》》》 ");
        double number2 = std.nextDouble();
       
        double sub = number1 - number2;
       
        String response;
       
        if(Math.abs(sub) >= 5.5){
            response = "二數相差大於或等於 5.5";
           
        }else{
            response = "二數相差小於 5.5";
        }
       
        System.out.println(response);

    }

}

jAG111


class MyAG111{
public static void main(String[] args){
int x = 10 ;
int y = 5;

if x == y
   System.out.println("相等。");
else
                    System.out.println("不相等。");
}
}

運用 if 判斷式的小程式,你能發現有幾個編譯錯誤??