题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

import java.util.*;

public class Main {

    /**
    1. 使用 scanner 的 hasNextLine 和 nextLine 接受一整行数据
    2. 用数组保存第一行的字符串和第二行的字符
    3. 定义 count,循环字符串,比对字符在其中出现的次数,出现 count+1
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<String> inputs = new ArrayList<>(2);
        while (scan.hasNextLine()) {
            inputs.add(scan.nextLine());
        }
        scan.close();

		// 统一转换成小写,以便于比较
        char[] strs = inputs.get(0).toLowerCase().toCharArray();
        char str = inputs.get(1).toLowerCase().charAt(0);
        int count = 0;
        for (char s:strs) {
            count += s == str ? 1:0;
        }
        System.out.println(count);
    }
}

全部评论

相关推荐

牛客90772103...:格林美(无锡)
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务