1. 程式人生 > >Thinking in Java 第四版完整版 第四章練習題

Thinking in Java 第四版完整版 第四章練習題

Thinking in Java 第四版完整版 第四章練習題,記錄一下(jdk1.8.0)

1.

/**
 * 練習1:寫一個程式,列印從1到100的值。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise401 {

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            System.out.print(i+"\t");
            if(i % 15
== 0) { System.out.println(); } } } }

這裡寫圖片描述

2.

import java.util.Random;

/**
 * 練習2:寫一個程式,產生25個int型別的隨機數。對於每一個隨機值,使用
 * if-else語句來將其分類為大於、小於或等於緊隨它而隨機生成的值。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise402 {

    public static void compare() {
        Random randonm = new
Random(); int a = randonm.nextInt(); int b = randonm.nextInt(); System.out.println("a = " + a + ", b = " + b); if(a > b) { System.out.println("a > b"); } else if (a == b) { System.out.println("a == b"); } else if (a < b) { System.out.println("a < b"
); } } public static void main(String[] args) { for (int i = 0; i < 25; i++) { compare(); } } }

這裡寫圖片描述

3.

import java.util.Random;

/**
 * 練習3:修改練習2,把程式碼用一個while無限迴圈包括起來。然後執行它直至用鍵盤中斷
 * 其執行(通常是通過按Ctrl-C)。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise403 {

    public static void compare() {
        Random randonm = new Random();
        int a = randonm.nextInt();
        int b = randonm.nextInt();
        System.out.println("a = " + a + ", b = " + b);
        if(a > b) {
            System.out.println("a > b");
        } else if (a == b) {
            System.out.println("a == b");
        } else if (a < b) {
            System.out.println("a < b");
        }
    }

    public static void main(String[] args) {
        while(true) {
            compare();
        }
    }
}

這裡寫圖片描述

4.

/**
 * 練習4:寫一個程式,使用兩個巢狀的for迴圈和取餘操作符(%)來探測和列印素數
 * (只能被其自身和1整除,而不能被其它數字整除的整數)。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise404 {

    public static void main(String[] args) {
        for (int i = 1; i < 100; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if(i % j == 0) {
                    flag = false;
                }
            }

            if(flag) {
                System.out.print(i + " ");
            }
        }
    }
}

這裡寫圖片描述

5.

/**
 * 練習5:重複第3章中的練習10,不要用Integer.toBinaryString()方法,而是用三元操作符
 * 和按位操作符來顯示二進位制的1和0。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise405 {

    public static String toBinaryString(int i) {
        char[] buffer = new char[32];
        int bufferPosition = 32;
        String res = "";
        do {
            buffer[--bufferPosition] = ((i & 0x01) != 0) ? '1' : '0';
            i = i >>> 1;
        } while(i != 0);

        for (int j = bufferPosition; j < buffer.length; j++) {
            res = res + buffer[j];
        }
        return res;
    }

    public static void main(String[] args) {
        int i1 = 0xaaaaaaaa;
        int i2 = 0x55555555;
        System.out.println("i1 = " + toBinaryString(i1));
        System.out.println("i2 = " + toBinaryString(i2));
        System.out.println("~i1 = " + toBinaryString(~i1));
        System.out.println("~i2 = " + toBinaryString(~i2));
        System.out.println("i1 & i2 = " + toBinaryString(i1 & i2));
        System.out.println("i1 | i2 = " + toBinaryString(i1 | i2));
        System.out.println("i1 ^ i2 = " + toBinaryString(i1 ^ i2));
        System.out.println("i1 & i1 = " + toBinaryString(i1 & i1));
        System.out.println("i1 | i1 = " + toBinaryString(i1 | i1));
        System.out.println("i1 ^ i1 = " + toBinaryString(i1 ^ i1));
        System.out.println("i2 & i2 = " + toBinaryString(i2 & i2));
        System.out.println("i2 | i2 = " + toBinaryString(i2 | i2));
        System.out.println("i2 ^ i2 = " + toBinaryString(i2 ^ i2));
    }
}

這裡寫圖片描述

6.

/**
 * 練習6:修改前兩個程式中的兩個test()方法,讓它們接收兩個額外的引數begin和end,
 * 這樣在測試testval時將判斷它是否在begin和end之間(包括begin和end)的範圍內。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise406 {

    static boolean test(int testval, int begin, int end) {
        if(testval >= begin && testval <= end) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(test(10, 5, 15));
        System.out.println(test(5, 10, 15));
        System.out.println(test(5, 5, 5));
    }
}

這裡寫圖片描述

7.

/**
 * 練習7:修改本章練習1,通過使用break關鍵詞,使得程式在列印到99時退出。
 * 然後嘗試使用return來達到相同的目的。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise407 {

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if(i == 99) {
                return; // break;
            }
            System.out.print(i+" ");
            if(i % 30 == 0) {
                System.out.println();
            }
        }
    }
}

這裡寫圖片描述

8.

/**
 * 練習8:寫一個switch開關語句,為每個case列印一個訊息。然後把這個switch放進for
 * 迴圈來測試每個case。先讓每個case後面都有break,測試一下會怎樣;然後把break刪了,
 * 看看會怎樣。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise408 {

    public static void main(String[] args) {
        for(int i = 0; i < 4; i++) {
            switch (i) {
            case 0:
                System.out.println("value is 0");
                //break;
            case 1:
                System.out.println("value is 1");
                //break;
            case 2:
                System.out.println("value is 2");
                //break;
            case 3:
                System.out.println("value is 3");
                //break;
            default:
                System.out.println("value is default");
            }
            System.out.println("-------------------------------");
        }
    }
}

這裡寫圖片描述

9.

/**
 * 練習9:一個斐波那契數列是由數字1、1、2、3、5、8、13、21、34等等組成的,其中每一個數字(從第三個數字起)
 * 都是前兩個數字的和。建立一個方法,接收一個整數引數,並顯示從第一個元素開始總共由該引數指定的個數所構成
 * 的所有斐波那契數字。例如,如果執行java Fibonacci 5(其中Fibonacci是類名),那麼輸出就應該
 * 是1、1、2、3、5。
 * @author admin11
 * @date 2018年3月1日
 */
public class Fibonacci {

    public static int fibonacci(int i) {
        if(i == 1) {
            return 1;
        } else if(i == 2) {
            return 1;
        } else {
            return fibonacci(i - 1) + fibonacci(i - 2);
        }
    }

    public static void main(String[] args) {
        int cnt = Integer.parseInt(args[0]);
        for(int i = 1; i <= cnt; i++) {
            System.out.print(fibonacci(i) + " ");
        }
    }
}

這裡寫圖片描述

10.

/**
 * 練習10:吸血鬼數字是指位數為偶數的數字,可以由一對數字相乘而得到,而這對數字各含乘積的一半位數
 * 的數字,其中從最初的數字中選取的數字可以任意排序。以兩個0結尾的數字是不允許的,例如,
 * 下列數字都是“吸血鬼”數字:
 * 1260 = 21 * 60
 * 1827 = 21 * 87
 * 2187 = 27 * 81
 * 寫一個程式,找出4位數的所有吸血鬼數字。
 * @author admin11
 * @date 2018年3月1日
 */
public class Exercise410 {

    public static void main(String[] args) {
        int[] startDigit = new int[4];
        int[] productDigit = new int[4];
        for (int num1 = 10; num1 <= 99; num1++) {
            for(int num2 = num1; num2 <= 99; num2++) {
                if((num1 * num2) %9 != (num1 + num2) % 9) {
                    continue;
                }
                int product = num1 * num2;
                startDigit[0] = num1 /10;
                startDigit[1] = num1 % 10;
                startDigit[2] = num2 / 10;
                startDigit[3] = num2 % 10;
                productDigit[0] = product / 1000;
                productDigit[1] = (product % 1000) / 100;
                productDigit[2] = product % 1000 % 100 / 10;
                productDigit[3] = product % 1000 % 100 % 10;

                int count = 0;
                for(int x = 0; x < 4; x++) {
                    for (int y = 0; y < 4; y++) {
                        if(productDigit[x] == startDigit[y]) {
                            count++;
                            productDigit[x] = -1;
                            startDigit[y] = -2;
                            if(count == 4) {
                                System.out.println(num1 + "*" + num2 + " : " + product);
                            }
                        }
                    }
                }
            }
        }
    }
}

這裡寫圖片描述