题解 | #字符串分隔#
计算某字符出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()) {
String s1=sc.nextLine().toUpperCase();//接收查询字符串转换为全大写
String s2=sc.nextLine().toUpperCase();//接收查询字符转换为大写
System.out.println(Ones(s1,s2));
}
}
//编写函数
public static int Ones(String s1, String s2) {
char[] arr = s1.toCharArray();//获得查询字符串对应的字符数组
int count =0;//计数器
for (int i = 0; i < arr.length; i++) {
if (s2.equals(arr[i]+"")){
count++;
}//遍历数组,比较
}
return count;//返回计数器
}
}