《Java程式設計經典300例》學習筆記

來源:互聯網
上載者:User

標籤:

一、基礎篇
1.不藉助第三者實現兩個變數值的交換:
package basic;

import java.util.Scanner;

public class ExchangeValue {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入變數A的值:");
        long a = scan.nextLong();
        System.out.println("請輸入變數B的值:");
        long b = scan.nextLong();
        System.out.println("a=" + a + "\tb=" + b);
        System.out.println("變數交換...\t");
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("a=" + a + "\tb=" + b);
    }
}

2.利用異或進行加解密:
package basic;

import java.util.Scanner;

public class Encryption {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("請輸入一個英文字串或解密字串");
        String password=scan.nextLine();
        char[] array=password.toCharArray();
        for(int i=0;i<array.length;++i){
            array[i]=(char)(array[i]^20000);
        }
        System.out.println("加密或解密結果如下:");
        System.out.println(new String(array));
    }
}

3.利用BigDecimal求和:
package basic;

import java.math.BigDecimal;

public class Factorial {
    public static void main(String[] args) {
        BigDecimal sum=new BigDecimal(0.0);
        BigDecimal factorial=new BigDecimal(1.0);
        int i=1;
        while(i<=10){
            sum=sum.add(factorial);
            ++i;
            factorial=factorial.multiply(new BigDecimal(1.0/i));
        }
        System.out.println("1+1/2!+1/3!+1/4!....+1/10!="+sum);
    }
}

4.for迴圈列印空心菱形:
package basic;

public class Diamond {
    public static void main(String[] args) {
        printDiamond(6);
    }

    public static void printDiamond(int size) {
        if (size % 2 == 0) {
            size++;
        }
        for (int i = 0; i < size / 2 + 1; ++i) {
            for (int j = size / 2 + 1; j > i + 1; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                if (j == 0 || j == 2 * i) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
        for (int i = size / 2 + 1; i < size; i++) {
            for (int j = 0; j < i - size / 2; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
                if (j == 0 || j == 2 * (size - i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
    }
}

5.猴子分桃問題:一堆桃子,5個猴子先後到,每次都均分5份,扔掉多餘的一個,拿走自己的那份。問最少有幾個桃子,最後一個猴子得到幾個桃子。
package basic;

public class MonkeyPeach {
    public static void main(String[] args) {
        int n = 1;
        int m = 0;
        int flag = 1;
        int monkeyNum = 5;
        while (true) {
            flag = 1;
            m = monkeyNum * n + 1;
            for (int i = monkeyNum; i >= 1; i--) {
                if ((m % (monkeyNum - 1) == 0)) {
                    m = m / (monkeyNum - 1) * monkeyNum + 1;
                    flag++;
                } else {
                    break;
                }
            }
            if (flag == monkeyNum)
                break;
            n++;
        }
        System.out.println("桃子總數為:" + m);
        System.out.println("第五個猴子得到的桃子數為:" + n);
    }
}

6.約瑟夫問題:
package basic;

import javax.swing.JOptionPane;

public class Josephus {
    public static void main(String[] args) {
        String s;
        int n, k, m, n1;
        s = JOptionPane.showInputDialog("請輸入人數:");
        n = Integer.parseInt(s);
        n1 = n + 1;
        s = JOptionPane.showInputDialog("請輸入起始報數的人的編號:");
        k = Integer.parseInt(s);
        s = JOptionPane.showInputDialog("請輸入死亡數字:");
        m = Integer.parseInt(s);
        int a[] = new int[n + 1];
        a[0] = 0;
        System.out.println("出局人的編號:");
        for (int i = 1; i < a.length; i++) {
            a[i] = 1;
        }
        for (int i = 1; i <= m; i++) {
            if (n == 1)
                break;
            else if (i == m) {
                n--;
                i = 0;
                a[k] = 0;
                System.out.print(k + " ");
            }
            do {
                k++;
                k = k % n1;
            } while (a[k] != 1);
        }
        System.out.println("");
        System.out.println("倖存者編號:" + k);
    }
}

7.數獨問題:
package basic;

import java.util.Scanner;

public class Sudoku {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("請輸入要計算的數獨維數(大於1的奇數):");
        int x = s.nextInt();
        int h = 0;
        int l = x / 2;
        int[][] a = new int[x][x];
        for (int i = 1; i <= x * x; i++) {
            a[h][l] = i;
            h--;
            l++;
            if (h < 0 && l >= x) {
                h += 2;
                l--;
            } else if (h < 0) {
                h = x - 1;
            } else if (l >= x) {
                l = 0;
            } else if (a[h][l] > 0) {
                h += 2;
                l--;
            }
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < x; j++)
                System.out.print(a[i][j] + " ");
            System.out.println();
        }
    }
}

8.數字格式轉為貨幣字串:
package basic;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;

public class CurrencyFormat {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個貨幣數字:");
        double number = scan.nextDouble();
        NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CHINA);
        System.out.println("Locale.CHINA:" + format.format(number));
        format = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println("Locale.US:" + format.format(number));
        format = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.println("Locale.UK:" + format.format(number));
    }
}

9.檢驗IP地址:

package basic;

import java.util.Scanner;

public class IPAddress {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入需要校正的IP地址:");
        String text = sc.nextLine();
        String info = matches(text);
        System.out.println(info);
    }

    public static String matches(String text) {
        if (text != null && !text.isEmpty()) {
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            if (text.matches(regex)) {
                return text + "是一個合法的IP地址!";
            } else {
                return text + "不是一個合法的IP地址!";
            }
        }
        return "請輸入要驗證的IP地址!";
    }
}

 

《Java程式設計經典300例》學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.