字符串的综合练习

1、练习-转换罗马数字

键盘录入一个字符串

要求1:长度为小于等于9

要求2:只能是数字

将内容变成罗马数字

下面是阿拉伯数字跟罗马数字的对比关系:

Ⅰ-1

Ⅱ-2

Ⅲ-3

Ⅳ-4

Ⅴ-5

Ⅵ-6

Ⅶ-7

Ⅷ-8

Ⅸ-9

注意点:罗马数字里面没有0的,如果键盘录入的数字包含0,可以变成”“(长度为0的字符串)

2、第一种解题思想(查表法)

package stringdemo;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String str;
        while (true) {
            System.out.println("请输入一个字符串");
            str = sc.next();
            boolean flag = checkStr(str);
            if (flag) {
                break;
            } else {
                System.out.println("当前的字符串不符合规则,请重新输入");
                continue;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int number = c - 48;
            String s = changeLuoMa(number);
            sb.append(s);
        }
        System.out.println(sb);


    }
    /*Ⅰ-1   Ⅱ-2     Ⅲ-3     Ⅳ-4     Ⅴ-5     Ⅵ-6     Ⅶ-7     Ⅷ-8    Ⅸ-9
*/
    public static String changeLuoMa(int number) {
        String[] arr = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
        return arr[number];
    }

    public static boolean checkStr(String string) {

        if (string.length() > 9) {
            return false;
        }

        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;

    }

}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2084:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.Test
请输入一个字符串
123456789
ⅠⅡⅢⅣⅤⅥⅦⅧⅨ

Process finished with exit code 0

3、第二种解题思想(switch)

package stringdemo;


import java.util.Scanner;

public class TestCase {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str;
        while (true) {
            System.out.println("请输入一个字符串");
            str = sc.next();
            boolean flag = checkStr(str);
            if (flag) {
                break;
            } else {
                System.out.println("当前的字符串不符合规则,请重新输入");
                continue;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            String s = changeLuoMa(c);
            sb.append(s);
        }
        System.out.println(sb);


    }

    /*Ⅰ-1   Ⅱ-2     Ⅲ-3     Ⅳ-4     Ⅴ-5     Ⅵ-6     Ⅶ-7     Ⅷ-8    Ⅸ-9
     */
    public static String changeLuoMa(char number) {

        String str = switch (number) {
            case '0' -> "";
            case '1' -> "Ⅰ";
            case '2' -> "Ⅱ";
            case '3' -> "Ⅲ";
            case '4' -> "Ⅳ";
            case '5' -> "Ⅴ";
            case '6' -> "Ⅵ";
            case '7' -> "Ⅶ";
            case '8' -> "Ⅷ";
            case '9' -> "Ⅸ";
            default -> str = "";
        };
        return str;
    }

    public static boolean checkStr(String string) {

        if (string.length() > 9) {
            return false;
        }

        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;

    }
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2435:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.TestCase
请输入一个字符串
123456789
ⅠⅡⅢⅣⅤⅥⅦⅧⅨ

Process finished with exit code 0

4、调正字符串

给定两个字符串,A和B

A的旋转操作就是将A最左边的字符移动到最右边。

例如,若A='abcde',在移动一次之后结果就是'bcdea'。

如果在若干次调整操作之后,A能变成B,那么返回True。

如果不能匹配成功,则返回false

4.1、如果我们看到要修改字符串的内容,可以有两种方法:

  1. 用subString进行截取,把左边的字符截取出来拼接到右侧去
  2. 可以把字符串先变成一个字符数组,然后调整字符数组里面数据,最后再把字符数组变成字符串。

4.1.1、第一种思路(subString)

​package stringdemo;

public class Case1 {
    public static void main(String[] args) {
        String strA = "abcde";
        String strB = "cdeab";

        boolean result = check(strA, strB);
        System.out.println(result);
    }
    public static boolean check(String strA, String strB) {
        for (int i = 0; i < strA.length(); i++) {
            strA = rotate(strA);
            if (strA.equals(strB)) {
                return true;
            }
        }
        return false;
    }

    public static String rotate(String str) {
        char first = str.charAt(0);
        String end = str.substring(1);
        return end + first;

    }
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=3417:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.Case1
true

Process finished with exit code 0




​

4.1.2、第二种思路

package stringdemo;

public class Case2 {
    public static void main(String[] args) {
        String strA = "abcde";
        String strB = "cdeab";

        boolean result = check(strA, strB);
        System.out.println(result);
    }
    public static boolean check(String strA, String strB) {
        for (int i = 0; i < strA.length(); i++) {
            strA = rotate(strA);
            if (strA.equals(strB)) {
                return true;
            }
        }
        return false;
    }

    public static String rotate(String str) {

        char[] arr = str.toCharArray();
        char first = arr[0];
        for (int i = 1; i < arr.length; i++) {
            arr[i - 1] = arr[i];
        }
        arr[arr.length - 1] = first;
        String result = new String(arr);
        return result;

    }
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=3381:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.Case2
true

Process finished with exit code 0

你可能感兴趣的:(#,常用API,java,开发语言)