Java字符串练习

发布时间 2023-04-20 11:24:47作者: harper886

Java字符串练习

题目要求

image-20230420100051591

代码实现

public class toArray {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        String res = fromArrayToString(arr);
        System.out.println(res);


    }

    public static String fromArrayToString(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                str += "world" + arr[i] + "#";
            } else {
                str += "world" + arr[i] + "]";
            }
        }


        return str;

    }


}

题目要求

image-20230420101040731

代码实现

import java.util.Scanner;

public class Count {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int countUpper = 0;
        int countNumber = 0;
        int countLower = 0;
        int countOther = 0;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 'A' && chars[i] <= 'Z') {
                countUpper++;
            } else if (chars[i] >= 'a' && chars[i] <= 'z') {
                countLower++;
            } else if (chars[i] >= '0' && chars[i] <= '9') {
                countNumber++;
            } else {
                countOther++;
            }

        }
        System.out.println("大写字母:" + countUpper);
        System.out.println("小写字母:" + countLower);
        System.out.println("数字:" + countNumber);
        System.out.println("其他字符:" + countOther);


    }
}