"Program 17"
Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.
1. Program Analysis: Using the while statement, the condition for the input character is not ' \ n '.
2. Program Source code:
1 ImportJava.util.Scanner;2 3 /* This article goes from the blog: www.cnblogs.com/java-1004 "program 17"5 Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters. 6 program analysis: converted to a char array, for the Order of judgement. 7 */8 9 Public classJava17 {Ten One Public Static voidMain (string[] args) { ASystem.out.print ("Please enter string:"); -Scanner Scanner =NewScanner (system.in); - Char[] arr =scanner.nextline (). ToCharArray (); the scanner.close (); - intLetters = 0, digit = 0, space = 0, others = 0; - for(inti = 0; i < arr.length; i++) { - if(Arr[i] >= ' 0 ' && arr[i] <= ' 9 ') { +digit++; -}Else if(Arr[i] = = ") { +space++; A}Else if(Arr[i] >= ' A ' && arr[i] <= ' Z ') { atletters++; -}Else if(Arr[i] >= ' A ' && arr[i] <= ' z ') { -letters++; -}Else { -others++; - } in } -System.out.print ("A total of Chinese and English letters" + Letters + "one, space" + Space + ", number" + digit + ", other characters" + others + "each")); to } +}
Java rewrite "C Classic 100 Questions"--17