标签:system static *** rtp self 个数 new 文字 10个
1 package test_1_6; 2 3 import java.util.Scanner; 4 5 public class CharType { 6 7 public static String numStr = ""; 8 public static int numCount = 0; 9 public static String letterStr = ""; 10 public static int letterCount = 0; 11 public static String otherStr = ""; 12 public static int otherCount = 0; 13 public static int spaceCount = 0; 14 15 public static void main(String[] args) { 16 17 /** 输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。 */ 18 19 Scanner sc = new Scanner(System.in); 20 System.out.println("请输入一个字符串:"); 21 /** 用\n作为Scanner中的分隔符,使空格可以被统计 */ 22 sc.useDelimiter("\n"); 23 String str = sc.next(); 24 25 countStrTpye(str); 26 27 System.out.println("英文字母总计" + letterCount + "个\n分别为:" + letterStr); 28 System.out.println("空格总计" + spaceCount + "个"); 29 System.out.println("数字总计" + numCount + "个\n分别为:" + numStr); 30 System.out.println("其它字符总计" + otherCount + "个\n分别为:" + otherStr); 31 32 33 } 34 35 private static void countStrTpye(String str) { 36 37 /** 转为字符数组 */ 38 char[] charArr = str.toCharArray(); 39 40 for (int i = 0; i < charArr.length; i++) { 41 judgeTpye(charArr[i]); 42 } 43 44 } 45 46 private static void judgeTpye(char c) { 47 48 int ascii = (int)c; 49 50 if (ascii >= 48 && ascii <= 59) { 51 numStr = numStr + c + " "; 52 numCount++; 53 } else if (ascii == 32) { 54 spaceCount++; 55 } else if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)){ 56 letterStr = letterStr + c + " "; 57 letterCount++; 58 } else { 59 otherStr = otherStr + c + " "; 60 otherCount++; 61 } 62 63 } 64 65 }
结果如下:
请输入一个字符串:
show me the money 192.168.1.1 Hello $$@@## World *** 10
英文字母总计24个
分别为:s h o w m e t h e m o n e y H e l l o W o r l d
空格总计9个
数字总计10个
分别为:1 9 2 1 6 8 1 1 1 0
其它字符总计13个
分别为:. . . $ $ @ @ # # * * *
[20-04-26][Self-test 6]Java CharType
标签:system static *** rtp self 个数 new 文字 10个
原文地址:https://www.cnblogs.com/mirai3usi9/p/12781897.html