Title: Enter the line of characters, each of which in English statistics , the number of letters, spaces, numbers and other characters.
Program Analysis: Using the while statement, the conditions for character input is not '\ n'.
public class Q7 statistical input letters spaces digits {
public static void main (String [] args) {
System.out.print ( "input line of characters" );
Scanner in = new new Scanner (System.in);
S String = in.nextLine ();
in.close ();
char [] = C s.toCharArray ();
int numOfChars = 0; // number of letters
int numOfSpaces = 0; // number of spaces
int numOfNumbers = 0 ; // number of digits
int numOfOthers = 0; // other number of characters
int i=c.length-1;
//遍历数组
while(i >-1) {
if(c[i] > 64 && c[i] <128 ) {
numOfChars++;
} else if(c[i]==' ') {
numOfSpaces++;
} else if(c[i] >47 &&(int) c[i] < 58) {
numOfNumbers++;
} else {
numOfOthers++;
}
i- ;
}
System.out.println ( "the number of letters as follows:" + numOfChars);
System.out.println ( "the number of spaces is:" + numOfSpaces);
System.out.println ( "digits as: "+ numOfNumbers);
System.out.println ( " the number of characters to the other: "+ numOfOthers);
}
}