I'm trying to solve this problem on CodingBat:
Return true if the given string contains between 1 and 3 'e' chars.
So far, I have this solution, but it won't compile if I have the "numofe" integer initialized inside the IF block, because other parts of the code won't work with it, and vice versa. So where should I initialize a variable, to be accessible everywhere in the code?
Thank you.
public boolean stringE(String str) {
int numofe;
for(int x = 0; str.length() > x; x++){
if (str.charAt(x)=='e'){
numofe++;
}
}
return (numofe>0 && numofe<4);
}
Aucun commentaire:
Enregistrer un commentaire