String at = "123123";
for (int i = 0; i <at.length() ; i++) {
if (at.charAt(i)==1){
int count=0;
count++;
}
}
想要1出现2次。哪里我的失败。谢谢。
答案 0 :(得分:2)
String at = "123123";
int count = 0; // Move out
for (int i = 0; i < at.length(); i++) {
if (at.charAt(i) == '1'){ // Compare with the char '1'
count++;
}
}
// count is 2 here.
答案 1 :(得分:0)
String at = "123123";
int count = 0;//outside the loop
int oneDigitCount = 0;
for (int i = 0; i < at.length(); i++) {
if (Character.isDigit(at.charAt(i))) {//digit count
count++;
}
if(at.charAt(i)=='1'){//count ones in string
oneDigitCount++;
}
}