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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        String textOne=scanner.nextLine();
        String textTwo=scanner.nextLine();
        textTwo=textTwo.toUpperCase();
        char[] chars = textTwo.toCharArray();
        char text2 = chars[0];
        int total=0;
        char xiaoxie;
        textOne= textOne.toUpperCase();//不管是有多少大写还是小写,统统转换成大写,
        
        for(int i=0;i<textOne.length();i++){
        if(text2<='Z'&&text2>='A'&&textOne.charAt(i)==text2){
            //说明是大写字母
            total++;
        }else if(text2>='0'&&text2<='9'&&textOne.charAt(i)==text2){
            //说明是数字
            total++;
        }
        }
        System.out.println(total);
    }
}



思路:不管原有要输入的字符串是有多少大写小写或者数字,统统转换成大写。
不管原有要输入的字符,是大写还是小写,统统转换成大写,因为题目里面说了,忽略大小写,所以干脆用大写来统一。
然后遍历字符串,因为asc码表里面,字符>='A'&&<= 'Z'就代表了大写,>='0'&&<='9'就代表了数字。
遍历判断是否相等,相等就++,得出结果
全部评论

相关推荐

刘湘_passion:出国旅游?那就小心你的腰子咯
点赞 评论 收藏
分享
喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务