题解 | #计算某字符出现次数#
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'就代表了数字。
遍历判断是否相等,相等就++,得出结果