本帖最后由 ash午夜阳光 于 2015-11-4 13:31 编辑
package day13;
public class Test {
public static void main(String[] args) {
tongJiEveryChar1("~``周杰伦~~!@@333$$林俊杰$3$%$33&%&^%$&$$$%^6哈哈Jay"); tongJiEveryChar2("~``周杰伦~~!@@333$$林俊杰$3$%$33&%&^%$&$$$%^6哈哈Jay");
}
public static void tongJiEveryChar1(String s) {
StringBuffer sb = new StringBuffer(s);
while (sb.length() != 0) {// sb里有字符就统计
int count = 0;// 记录每个字符个数
char ch = sb.charAt(0);// 头个字符赋给ch
int index;// 记录所找字符位置
while ((index = sb.indexOf(String.valueOf(ch))) >= 0) {// 若找到字符
sb.deleteCharAt(index);// 从sb中删除
count++;// 计数器自增
}
System.out.println(ch + "(" + count + ")");// 打印结果
}
}
static void tongJiEveryChar2(String s) {
StringBuffer temp = new StringBuffer();
// 获取无重字符串
for (int i = 0; i < s.length(); i++) {
if (temp.indexOf(String.valueOf(s.charAt(i))) < 0) {
temp.append(s.charAt(i));
}
}
// 统计无重字符串中每个字符个数
for (int i = 0, count = 0; i < temp.length(); i++, count = 0) {
// 无重字符串中每个字符,都在元字符串中查找一遍
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == temp.charAt(i)) {
count++;
}
}
// 输出结果
System.out.println(temp.charAt(i) + "(" + count + ")");
}
}
}
|
|