题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import java.util.*; public class Test{ public static void main(String args[]){ int letterCount=0; int spaceCount=0; int numCount=0; int otherCount=0; //定义一个扫描器扫描控制台输入的信息 Scanner scan = new Scanner(System.in); System.out.println("Please input a String"); String str = scan.nextLine(); //把字符串转化为字符数组 char[] chars = str.toCharArray(); for(int i=0;i<chars.length;i++){ if(Character.isLetter(chars[i])) letterCount++; else if(Character.isDigit(chars[i])) numCount++; else if(Character.isSpaceChar(chars[i])) spaceCount++; else otherCount++; } System.out.println("letterCount is: "+letterCount); System.out.println("numCount is: "+numCount); System.out.println("spaceCount is: "+spaceCount); System.out.println("otherCount is: "+otherCount); } }