輸入一個字符串遇到空格的時候就出問題,求解答
public static void main(String[] args){
int e = 0;
int n = 0;
int s = 0;
int o = 0;
Scanner scan = new Scanner(System.in);
System.out.print("請輸入一竄字符:");
String str = scan.next();
for(int i=0;i
char ch = str.charAt(i);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
e++;
}else if(ch==' '){
s++;
}else if(ch<='9'&&ch>='0'){
n++;
}else{
o++;
}
}
System.out.println("字母個數:"+e);
System.out.println("數字個數:"+n);
System.out.println("空格個數:"+s);
System.out.println("其他個數:"+o);
列如輸入ff 55 輸出字母個數:2 其他都為0,這是為什麼
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();