知识点:常用String的indexOf和substring组合方法来求字符串中某个字符出现的次数
统一一个字符串中每个字符的个数。输出结果按在字符串中出现的先后顺序输出。
输入格式:
输入一个字符串。
输出格式:
输出结果按在字符串中出现的先后顺序输出。
输入样例:
在这里给出一组输入。例如:
qqqww222weeeerrqqqtttttt111我爱爱爱爱爱你
输出样例:
在这里给出相应的输出。例如:
q: 6
w: 3
2: 3
e: 4
r: 2
t: 6
1: 3
我: 1
爱: 5
你: 1
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
ArrayList<String> list=new ArrayList();
String[] array=str.split("");
//containts去重
for(int j=0;j<str.length();j++) {
if(!list.contains(array[j])) {
list.add(array[j]);
}
}
//统计个数
int[] num=new int[list.size()];
for(int i=0;i<list.size();i++) {
num[i]=getCount(str,list.get(i));
System.out.println(list.get(i)+": " + num[i]);
}
}
//统一一个字符串中每个字符的个数
public static int getCount(String str,String str1){
int count = 0;
int index = 0;
//截取+索引定位
while(((index = str.indexOf(str1)) != -1)){
str = str.substring(index+1);
count++;
}
return count;
}
}
求解其它方法