双体系项目部寒假部分作业(二)

编写程序统计字符串“The String class represents character strings”中字母“s”的出现次数,区分大小写。


package com.stx.p2;

/**
 * @Author HanLin
 * @Date 2021/2/10 11:29
 * @Version 1.0
 */
public class test02 {
    public static void main(String[] args) {
        String txt = "The String class represents character strings";
        char[] arr = txt.toCharArray();//字符串转数组
        int S_count = 0;
        int s_count = 0;
        //本来打算用三元运算符连写两个 结果发现java不支持,但是js是支持的
        for (int x : arr) {
            if (x == 'S') {
                S_count++;
            } else if (x == 's') {
                s_count++;
            }
        }
        System.out.println("S出现次数"+S_count +"\ns出现次数"+ s_count);
    }
}

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注