请自行编写一个计算字符电长度的方法 int myStringLength ( String s ),要来不能使用 Java 类库提供的 length ()方法,并在 main 方法中来测试该方法。
import java.util.Scanner;
public class changdu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String s=in.next();
int a=myStringLength(s);
System.out.println(a);
}
public static int myStringLength(String s) {
char[] sr=s.toCharArray();
int count=0,a=0,b=0,c=0;
char [] st=s.toCharArray();
for(int i=0;i<10;i++) {
if((st[i]>='a' && st[i]<='z' )||(st[i]>='A' && st[i]<='Z')) {
a++;
}
if(st[i]==' ') {
b++;
}
if(st[i]>='0' && st[i]<='9') {
c++;
}
}
return a+b+c;
}
}
我这个运行会报错,
或者这有没有更好的写法