从键盘中随意输入一串字符,统计并输出该字符串中各字符(数字、大写字母、小写字母、标点符号等)各自出现的次数。
用面向对像的思想实现。
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scnn = new Scanner(System.in); System.out.println("请输入一段字符串"); String str = scnn.next(); char[] array = str.toCharArray(); sumWord(array); } public static void sumWord(char[] array) { int countNumber = 0; int countBigWord = 0; int countSmallWord = 0; int countOthers = 0; for (int i = 0; i < array.length; i++) { if (array[i] >= '0' && array[i] <= '9') countNumber++; else if (array[i] >= 'a' && array[i] <= 'z') { countSmallWord++; } else if (array[i] >= 'A' && array[i] <= 'Z') { countBigWord++; } else { countOthers++; } } System.out.println("数字有:" + countNumber); System.out.println("大写字母有:" + countBigWord); System.out.println("小写字母有:" + countSmallWord); System.out.println("其他字符有:" + countOthers); } }
从键盘中随意输入一串字符,统计并输出该字符串中各字符(数字、大写字母、小写字母、标点符号等)各自出现的次数。用面向对像的思想实现。
最新推荐文章于 2024-07-27 17:56:15 发布