第1题,给定 char[] word={ 'a ' , 'A' ,'a ' , 'A', 'b ' ,'c '},统计给定字符串a或者A的个数。
解题思路:挺简单的题目,直接无脑遍历数组进行比较即可,代码如下:
public class Demo1 {
/*
char[] word={ 'a ' , 'A' ,'a ' , 'A', 'b ' ,'c '};
统计给定字符串a或者A的个数
*/
public static void main(String[] args) {
char[] word={ 'a' , 'A' ,'a' , 'A', 'b' ,'c'};
int count=0;
for(int i=0;i<word.length;i++) {
if(word[i]=='a'||word[i]=='A') {
count++;
}
}
System.out.println("字符串中a和A一共出现了"+count+"次");
}
}