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

计算某字母出现次数

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

坑点在于
Scanner 接收 next的时候不能出现空格,否则空格被当做分隔符,需要用nextLine接收整行

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
         String str = null;
         if(in.hasNext()){
             str = in.nextLine();
             str = str.toLowerCase();
         }
        char key = 'a';
        if (in.hasNext()){
            String input = in.nextLine();
            key = input.toLowerCase().toCharArray()[0];
        }
        int count = 0;
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if (c == key){
                count++;
            }
        }
        System.out.println(count);
    }
}
全部评论

相关推荐

湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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