1 ImportJava.util.Scanner;2 3 /**4 * Enter a line of characters to count the number of Chinese and English letters, spaces, numbers, and other characters, respectively .5 *6 * @authorXCX7 * @time July 22, 2017 morning 9:08:168 */9 Public classPractice7 {Ten One Public Static voidMain (string[] args) { ASystem.out.println ("Please enter a string, we will count the number of Chinese and English letters, spaces, numbers and other characters"); -Scanner Scanner =NewScanner (system.in); -String string =scanner.nextline (); the - intNumbersofblack =Getnumbersofblack (string); - intNumbersofchinese =Getnumbersofchinese (string); - intNumbersofothers =getnumbersofothers (string); + intNumbersoffigure =getnumbersoffigure (string); - intNumbersofenglish =getnumbersofenglish (string); + ASystem.out.println ("Number:" + numbersoffigure + "X")); atSystem.out.println ("English:" + numbersofenglish + "one")); -SYSTEM.OUT.PRINTLN ("spaces are:" + numbersofblack + "X")); -System.out.println ("Kanji:" + Numbersofchinese + "one")); -System.out.println ("Others are:" + numbersofothers + "X")); - } - in //to find the number of hollow lattice of a string - Public Static intGetnumbersofblack (String string) { to //record the number of empty cells in a string + intNumbersofblack = 0; - //Converts the input string into a character array theString[] strings = String.Split (""); * for(inti = 0; i < strings.length; i++) { $ if(Strings[i].matches ("")) {Panax Notoginsengnumbersofblack++; - } the } + returnNumbersofblack; A } the + //to find the number of Chinese characters in a string - Public Static intGetnumbersofchinese (String string) { $ //record the number of Chinese characters in a string $ intNumbersofchinese = 0; - //Converts the input string into a character array -String[] strings = String.Split (""); the for(inti = 0; i < strings.length; i++) { - if(Strings[i].matches ("[\u4e00-\u9fa5]")) {Wuyinumbersofchinese++; the } - } Wu returnNumbersofchinese; - } About $ //find the number of English letters in a string - Public Static intgetnumbersofenglish (String string) { - //the number of English letters in the record string - intNumbersofenglish = 0; A //Converts the input string into a character array +String[] strings = String.Split (""); the for(inti = 0; i < strings.length; i++) { - if(Strings[i].matches ("[A-za-z]")) { $numbersofenglish++; the } the } the returnNumbersofenglish; the } - in //Number of numbers in a string the Public Static intgetnumbersoffigure (String string) { the //Number of digits in the record string About intNumbersoffigure = 0; the //Converts the input string into a character array theString[] strings = String.Split (""); the for(inti = 0; i < strings.length; i++) { + if(Strings[i].matches ("[0-9]")) { -numbersoffigure++; the }Bayi } the returnnumbersoffigure; the } - - //Number of numbers in a string the Public Static intgetnumbersofothers (String string) { the //Number of digits in the record string the intNumbersofothers = String.Split (""). Length the-Getnumbersofblack (String)-Getnumbersofchinese (String) --Getnumbersofenglish (String)-getnumbersoffigure (string); the returnnumbersofothers; the } the}
Java Learning (6): Enter a string that counts the number of Chinese and English letters, spaces, numbers, and other characters, respectively.