题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
使用Book数组存储每个字符出现的次数,回车的输入Scanner.nextLine()不会读入。
import java.util.Scanner;
public class Program {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int[] book = new int[256];
for ( int i = 0; i < str.length(); i++ ) {
int index = str.charAt(i);
book[index]++;
}
for ( int i = 0; i <= 255; i++ ) {
if ( book[i] != 0 ) {
char ch = (char)i;
System.out.println(ch + "出现了" + book[i] + "次!");
}
}
}
}
本文介绍了一个Java程序,该程序能够接收用户输入的一行字符,并统计其中英文字母、空格、数字和其他字符的出现次数。通过使用数组存储字符频率,程序遍历字符串并更新对应字符的计数。
4204

被折叠的 条评论
为什么被折叠?



