怎么老是報(bào)異常
public class ZiFuChuan{
? ? public static void main(String[] args) {
// 定義一個(gè)字符串
String s = "aljlkdsflkjsadjfklhasdkjlflkajdflwoiudsafhaasdasd";
? ? ? ??
? ? ? ? // 出現(xiàn)次數(shù)
int num = 0;
? ? ? ??
? ? ? ? ?// 循環(huán)遍歷每個(gè)字符,判斷是否是字符 a ,如果是,累加次數(shù)
for (int i=0;i<=s.length();i++ )
{
? ? ? ? ? ? // 獲取每個(gè)字符,判斷是否是字符a
if (s.charAt(i) == 'a'){
? ? ? ? ? ? ? ? // 累加統(tǒng)計(jì)次數(shù)
num++;?
}
}
System.out.println("字符a出現(xiàn)的次數(shù):" + num);
}
}
2018-09-14
i<=s.length()? 這有錯(cuò)誤,因?yàn)閿?shù)組和字符串都是從0開始計(jì)算,應(yīng)該i<s.length() 或者 i<= s.length()-1
2018-09-01
上面說(shuō)的對(duì),你這樣寫會(huì)報(bào)數(shù)組下標(biāo)越界的異常
2018-07-30
i<=s.length()? 這有錯(cuò)誤,因?yàn)閿?shù)組中以【0】開始,而字符串從1開始計(jì)算!應(yīng)該i<s.length() 或者 i<= s.length()-1