package dyh3_5_3;
class TestDemo {
private int count = 0; // 要查询的字符出现的次数
private char zm; // 定义要查询的字符
private String str; // 定义要被搜索的字符串
public TestDemo(String str, char zm) {
this.str = str;
this.zm = zm;
}
//定义function方法 将字符串中的每个字符依次与要查询的字符比较是否相同
public void function() {
for( int i = 0; i < str.length(); i++) {
char cm = str.charAt(i);
if( cm == zm ){
count++;
}
}
System.out.println("字符: " + zm + "出现的次数为:" + count + "次");
}
}
public class Love{
public static void main(String[] args) {
// 查询 i 字母在字符串中出现的次数
TestDemo yx = new TestDemo("i love yang xiao and i would protect her for whole my life!", 'i');
yx.function();
}
}