题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//.toLowerCase():将读取到的输入转换为小写形式
String str=sc.nextLine().toLowerCase();
//输入字符
char c=sc.nextLine().toLowerCase().charAt(0);
//报错的原因是因为 sc.nextLine() 返回的是一个字符串,而不是单个字符,
//而 toLowerCase() 方法是用于字符串类型的。因此,你不能将一个字符串赋值给一个
//字符类型的变量。
int count=0;//用于结果计数
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==c)
{count++;}
}
System.out.println(count);
}
}

