Java-黑马Java学习练习-day10字符串

  • Post author:
  • Post category:java


学习视频链接:

https://www.bilibili.com/video/BV17F411T7Ao




练习1. 用户登录

​ 已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示

package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice01
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习1,用户登录
 */
public class codePractice01 {
    public static void main(String[] args) {
        String userName = "kox";
        String password = "Kox123";
        Scanner sc = new Scanner(System.in);
        int count = 3;
        while (count > 0) {
            System.out.print("输入用户名:");
            String user = sc.next();
            System.out.print("输入密码:");
            String pd = sc.next();
            if (user.equals(userName) && pd.equals(password)) {
                System.out.printf("欢迎%s, 登录成功!", user);
                System.out.println();
                break;
            } else {
                count--;
                System.out.printf("登录失败,还有%s次机会", count);
                System.out.println();
            }
        }
    }
}



练习2. 遍历字符串

​ 键盘录入一个字符串,使用程序实现在控制台遍历该字符串

package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice02
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习2,遍历字符串
 */
public class codePractice02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
        }

    }
}



练习3. 统计字符串次数

​ 键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)

package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice03
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 统计字符次数
 */
public class codePractice03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入一个字符:");
        String str = sc.next();
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 'a' && c <= 'z') {
                smallCount++;
            } else if (c >= 'A' && c <= 'Z') {
                bigCount++;
            } else if (c >= 48 && c <= 57) {
                numberCount++;
            }
        }
        System.out.printf("bigCount;%s, smallCount:%s, numberCount:%s", bigCount, smallCount, numberCount);

    }
}



练习4. 字符串拼接

​ 定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,

​ 并在控制台输出结果。例如,数组为 int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3]

package cn.kox.practice;

/**
 * @ClassName: codePractice04
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习4,拼接字符串
 */
public class codePractice04 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3 ,4};
        System.out.println(arrToString(arr));
    }

    public static String arrToString(int[] arr) {
        if (arr == null) {
            return "";
        }

        if (arr.length == 0) {
            return "[]";
        }

        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                result += arr[i] + "]";
            } else {
                result += arr[i] + ",";
            }
        }
        return result;
    }
}



练习5. 字符串反转

​ 定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果

​ 例如,键盘录入 abc,输出结果 cba

package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice05
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习5,字符串反转
 */
public class codePractice05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入一个字符串:");
        String str = sc.next();
        System.out.println(reversalString(str));

    }

    public static String reversalString(String str) {
        String result = "";
        for (int i = str.length()-1; i > -1; i--) {
            result += str.charAt(i);
        }
        return result;
    }
}



练习6. 金额转换

​ 把2135变成:零佰零拾零万贰仟壹佰叁拾伍元

​ 把789变成:零佰零拾零万零仟柒佰捌拾玖元

package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice06
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习6,金额转换
 */
public class codePractice06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int monkey;
        while (true) {
            System.out.print("请录入一个金额:");
            monkey = sc.nextInt();
            if (monkey >= 0 && monkey <= 9999999) {
                break;
            } else {
                System.out.println("金额无效");
            }
        }

        String moneyStr = "";
        while (true) {
            int ge = monkey % 10;
            String capitalNumber = getCapitalNumber(ge);
            moneyStr = capitalNumber + moneyStr;
            monkey = monkey / 10;
            if (monkey == 0) {
                break;
            }
        }

        int count = 7 - moneyStr.length();
        for (int i = 0; i < count; i++) {
            moneyStr = "零" + moneyStr;
        }
        System.out.println(moneyStr);
        String[] arr = {"佰", "拾", "万", "迁", "佰", "拾", "元"};
        String result = "";
        for (int i = 0; i < moneyStr.length(); i++) {
            char c = moneyStr.charAt(i);
            result = result + c + arr[i];
        }
        System.out.println(result);


    }

    public static String getCapitalNumber(int number) {
        String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆" , "柒", "捌", "玖"};
        return arr[number];
    }
}



练习7. 手机号屏蔽

需求:以字符串的形式从键盘接受一个手机号,将中间四位号码屏蔽

最终效果为:

131****9468

package cn.kox.practice;

/**
 * @ClassName: codePractice07
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习7,手机号屏蔽
 */
public class codePractice07 {
    public static void main(String[] args) {
        String phoneNumber = "13112349468";

        String start = phoneNumber.substring(0, 3);
        String end = phoneNumber.substring(7, 11);
        phoneNumber = start + "****" + end;
        System.out.println(phoneNumber);
    }
}



练习8. 身份证信息查看

​ 身份证的每一位都是有固定的含义:

  • 1、2位:省份
  • 3、4位:城市
  • 5、6位:区县
  • 7-14位:出生年、月、日
  • 15、16位:所在地派出所
  • 17位:性别(奇数男性,偶数女性)
  • 18位:个人信息码(随机产生)

要求打印内容方式如下:

​ 人物信息为:

​ 出生年月日:XXXX年X月X日

​ 性别为:男/女

package cn.kox.practice;

/**
 * @ClassName: codePractice08
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习8,身份证信息查看
 */
public class codePractice08 {
    public static void main(String[] args) {
        String id = "321281202001011234";

        // 获取出生年月日
        String year = id.substring(6, 10);
        String month = id.substring(10, 12);
        String day = id.substring(12, 14);

        System.out.println("人物信息为:");
        System.out.println("出生年月日:" + year + "年" + month + "月" + day + "日");

        char gender = id.charAt(16);
        int num = gender - 48;
        if (num % 2 == 0 ) {
            System.out.println("性别为:女");
        } else {
            System.out.println("性别为:男");
        }

    }
}



练习9. 敏感词替换

需求1:键盘录入一个 字符串,如果字符串中包含(TMD),则使用***替换

package cn.kox.practice;

/**
 * @ClassName: codePractice09
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习9,敏感词替换
 */
public class codePractice09 {
    public static void main(String[] args) {
        String talk = "你玩的真好,以后不要再玩了,fuck";

        String[] arr = {"TMD", "CNM", "SB", "bitch", "fuck"};

        for (int i = 0; i < arr.length; i++) {
            talk = talk.replace(arr[i], "***");
        }

        System.out.println(talk);
    }
}



练习10. 对称字符串

需求:

​ 键盘接受一个字符串,程序判断出该字符串是否是对称字符串,并在控制台打印是或不是

对称字符串:123321、111

非对称字符串:123123
package cn.kox.practice;

import java.util.Scanner;

/**
 * @ClassName: codePractice10
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习10,对称字符串
 */
public class codePractice10 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String str = sc.next();

        String result = new StringBuilder().append(str).reverse().toString();
        System.out.println(str.equals(result));
    }
}



练习11. 拼接字符串

需求:定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回。

​ 调用该方法,并在控制台输出结果。

​ 例如:数组为int[] arr = {1,2,3};

​ 执行方法后的输出结果为:[1, 2, 3]

package cn.kox.practice;

/**
 * @ClassName: codePractice11
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习11,拼接字符串
 */
public class codePractice11 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        String result = arrToString(arr);
        System.out.println(result);
    }

    public static String arrToString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i] + "]");
            } else {
                sb.append(arr[i]).append(", ");
            }
        }
        return sb.toString();
    }
}



练习12. 罗马数字

  • 解法一
package cn.kox.practice;

import java.util.Scanner;
import java.util.StringJoiner;

/**
 * @ClassName: codePractice12
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习12,罗马数字
 */
public class codePractice12Case1 {
    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("输入错误!");
            }
        }
        StringJoiner sj = new StringJoiner(",", "[", "]");
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int number = c - 48;
            String s = changeLuoMa(number);
            sj.add(s);
        }
        System.out.println(sj);
    }


    public static String changeLuoMa(int number) {
        String[] arr = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
        return  arr[number];
    }

    public static boolean checkStr(String str) {
        if (str.length() > 9) {
            return false;
        }

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

  • 解法二
package cn.kox.practice;

import java.util.Scanner;
import java.util.StringJoiner;

/**
 * @ClassName: codePractice12Case02
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch:
 */
public class codePractice12Case2 {
    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("输入错误!");
            }
        }
        StringJoiner sj = new StringJoiner(",", "[", "]");
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            String s = changeLuoMa(c);
            sj.add(s);
        }
        System.out.println(sj);
    }


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

    public static boolean checkStr(String str) {
        if (str.length() > 9) {
            return false;
        }

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



练习13. 调整字符串

  • 解法一
package cn.kox.practice;

/**
 * @ClassName: codePractice13
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch: 练习13,调整字符串
 */
public class codePractice13Case1 {
    public static void main(String[] args) {
        String strA = "abcde";
        String strB = "cdeabd";

        System.out.println(check(strA, strB));
    }

    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;
    }
}

  • 解法二
package cn.kox.practice;

/**
 * @ClassName: codePracticeCase2
 * @Author: Kox
 * @Data: 2023/1/20
 * @Sketch:
 */
public class codePractice13Case2 {
    public static void main(String[] args) {
        String strA = "abcde";
        String strB = "cdeab";

        System.out.println(check(strA, strB));
    }

    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;
    }
}



版权声明:本文为Kox_233原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。