10 统计字符串

    xiaoxiao2025-08-10  8

    //输入一行字符,分别统计 //分析:这个就是如果出现换行的话就不行,就说明不能识别 public class Seven7Statistics { public static void main(String[] args) { // TODO Auto-generated method stub //键盘输入 Scanner sc=new Scanner(System.in); String str=sc.nextLine(); statistics(str); } //定义统计字符串的代码 public static void statistics(String str) { //我们可以把字符插入到队列里面,然后将这些字符放入字符数组里面,接着用首尾相减法,得出字符 List<String> list=new ArrayList<String>(); //静态数组的定义数组的定义 type[] arrayname={,,,,,}; //动态初始化 arrayname=new type[length]; //查阅API可知,toCharArray是转换为一个新的数组 char[] arrayofchar=str.toCharArray(); //遍历这个数组 for(char c: arrayofchar) { list.add(String.valueOf(c)); } //遍历排序后的List集合,并进行排序 Collections.sort(list); for(String s:list) { //定义第一次出现的索引 // indexOf(Object o) // 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 int start=list.indexOf(s); //同理 int end=list.lastIndexOf(s); //接着统计字符串的个数 int count=(end-start+1); if(list.get(end) ==s){ //输出字符数 System.out.println("字符"+s+":有多少个"+count+"个"); } } } } 出其中英文字母、空格、数字和其它字符的个数。
    转载请注明原文地址: https://ju.6miu.com/read-1301604.html
    最新回复(0)