Java 實現 C 語言經典 100 例(31 - 35)

1. 實例 31

1.1 題目

請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續判斷第二個字母。

1.2 思路

利用條件語句先對第一個字符進行匹配,然後再對需要第二個字符進行匹配的進行判斷

1.3 代碼

import java.util.Scanner;

/**
 * @ClassName : ThirtyOne
 * @Author : cunyu
 * @Date : 2020/6/25 12:07
 * @Version : 1.0
 * @Description : 實例 31
 **/

public class ThirtyOne {
    public static void main(String[] args) throws Exception {
        System.out.println("輸入字符:");
        // 輸入第一個知乎
        Scanner input = new Scanner(System.in);
        char cha = input.next().charAt(0);

        // 對第一個字符進行匹配
        switch (cha) {
            case 'm':
                System.out.println("Monday");
                break;
            case 'w':
                System.out.println("Wednesday");
                break;
            case 'f':
                System.out.println("Friday");
                break;
            // 需要輸入第二個字符的兩種情況
            case 't':
                System.out.println("繼續輸入");
                char tmp1 = input.next().charAt(0);
                if (tmp1 == 'u') {
                    System.out.println("Tuesday");
                } else if (tmp1 == 'h') {
                    System.out.println("Thursday");
                }
                break;
            case 's':
                System.out.println("繼續輸入");
                char tmp2 = input.next().charAt(0);
                if (tmp2 == 'a') {
                    System.out.println("Saturday");
                } else if (tmp2 == 'u') {
                    System.out.println("Sunday");
                }
                break;
            default:
                System.out.println("輸入錯誤");
                break;
        }
    }
}

1.4 結果

輸入字符:
t
繼續輸入
h
Thursday

2. 實例 32

2.1 題目

刪除一個字符串中的指定字母,如:字符串 “aca”,刪除其中的 a 字母。

2.2 思路

見代碼註釋即可

2.3 代碼

import java.util.Scanner;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : ThirtyTwo
 * @date : 2020/6/28 11:32
 * @description : 實例 32
 */

public class ThirtyTwo {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);

        // 分別輸入字符串和要刪除的字符
        System.out.println("輸入字符串");
        String str = input.nextLine();

        System.out.println("輸入要刪除的字符");
        char cha = input.next().charAt(0);

        System.out.println("刪除字符後的字符串:\n" + deleteCharacter(str, cha));

    }

    public static String deleteCharacter(String str, char cha) {
        // 將要刪除的字符替換爲 ""
        return str.replaceAll(cha + "", "");
    }
}

2.4 結果

輸入字符串
hello wrold!
輸入要刪除的字符
o
刪除字符後的字符串:
hell wrld!

3. 實例 33

3.1 題目

判斷一個數字是否爲質數。

3.2 思路

質數(prime number)又稱素數,有無限個。一個大於1的自然數,除了1和它本身外,不能被其他自然數整除。

3.3 代碼

import java.util.Scanner;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : ThirtyThree
 * @date : 2020/6/28 11:50
 * @description : 實例 33
 */

public class ThirtyThree {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("輸入一個數");
        int number = input.nextInt();
        System.out.format("%d 是素數?:%s\n", number, prime(number));
    }

    public static boolean prime(int num) {

        if (num==1){
            return false;
        }

        // 一個數只有 1 和它本身的約數,則該數是素數
        for (int i = 2; i <= (int) Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }

        return true;
    }
}

3.4 結果

輸入一個數
8
8 是素數?:false

4. 實例 34

4.1 題目

練習函數調用。

4.2 思路

C 語言中的函數調用即 Java 中的方法調用,在前面的實例中已經演示過

4.3 代碼

/**
 * @author : cunyu
 * @version : 1.0
 * @className : ThirtyFour
 * @date : 2020/6/28 12:05
 * @description : 實例 34
 */

public class ThirtyFour {
    public static void main(String[] args) throws Exception {
        String name = "村雨遙";
        ThirtyFour thirtyFour = new ThirtyFour();
        thirtyFour.greet(name);
    }

    public void greet(String name) {
        System.out.println("歡迎來到 Java 的 世界," + name);
    }
}

4.4 結果

歡迎來到 Java 的 世界,村雨遙

5. 實例 35

5.1 題目

字符串反轉,如將字符串 “www.runoob.com” 反轉爲 “moc.boonur.www”

5.2 思路

見代碼註釋即可

5.3 代碼

import java.util.Arrays;
import java.util.Collections;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : ThirtyFive
 * @date : 2020/6/28 12:25
 * @description : 實例 35
 */

public class ThirtyFive {
    public static void main(String[] args) throws Exception {
        String str = "https://cunyu1943.github.io";
        System.out.println("反轉前的字符串:");
        System.out.println(str);
        System.out.println("反轉後的字符串:");
        // 調用 reverse() 方法
        System.out.println(new StringBuffer(str).reverse().toString());
    }
}

5.4 結果

反轉前的字符串:
https://cunyu1943.github.io
反轉後的字符串:
oi.buhtig.3491uynuc//:sptth

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章