public class TestString {
public static void main(String[] args) {
String str = "abcabcabc" ; //统计该字符串中出现了哪些字符,并且把每个字符出现的次数打印输出。
aaaa(str) ;
}
public static void aaaa(String str) { for (int i = 0; i < str.length(); i++) {
char c1 = str.charAt(i);
int count = 0;
for (int j = i; j < str.length(); j++) {
char c2 = str.charAt(j) ;
if (c1 == c2) {
count++;
} else {
continue;
}
}
str = str.replaceAll(String.valueOf(c1), "") ;
System.out.println(c1 + "出现了" + count + "次");
}
}
}
public static void main(String[] args) {
String str = "abcabcabc" ; //统计该字符串中出现了哪些字符,并且把每个字符出现的次数打印输出。
aaaa(str) ;
}
public static void aaaa(String str) { for (int i = 0; i < str.length(); i++) {
char c1 = str.charAt(i);
int count = 0;
for (int j = i; j < str.length(); j++) {
char c2 = str.charAt(j) ;
if (c1 == c2) {
count++;
} else {
continue;
}
}
str = str.replaceAll(String.valueOf(c1), "") ;
System.out.println(c1 + "出现了" + count + "次");
}
}
}
解决方案 »
- 怎样做个exe文件来启动我的java程序
- 请问如何把float指定位数进行四舍五入?
- zip 压缩遇到问题
- mina 过滤器与编码器区别?
- 灵异事件!在oracle sql*plus里执行得好好的insert语句,到java里用statement.executeupdate()就不行了!?
- 在Applet里面如何打开IE浏览器?
- 谁有JB数据库完整的小程序,请发来,不胜感谢!
- 求个算法,面试题来着。
- 看过jbuilder5应用开发详解并且对rmi编程干兴趣的请看这里:
- IE6无法显示Applet,AppletViewer中可正常显示,亦安装了Java Plug In,不知是何原因
- 怎么重画JInternalFrame
- 我一直不太明白的一个关于继承方面的问题
//方法1:
public class TestString {
public static void main(String[] args) {
String str = "abcabcabc" ; //统计该字符串中出现了哪些字符,并且把每个字符出现的次数打印输出。aaaa(str) ;}public static void aaaa(String str)
{for (int i = 0; i < str.length(); i++)
{
//////////
i = 0;///////
///////////////
char c1 = str.charAt(i);
int count = 0;
for (int j = i; j < str.length(); j++)
{
char c2 = str.charAt(j) ;
if (c1 == c2) {
count++;
} else {
continue;
}
}
str = str.replaceAll(String.valueOf(c1), "") ;
System.out.println(c1 + "出现了" + count + "次");
}}
}
//方法2
public static void aaaa(String str)
{while(str.length()>0){
char c1 = str.charAt(0);
int count = 0;
for (int j = 0; j < str.length(); j++)
{
char c2 = str.charAt(j) ;
if (c1 == c2) {
count++;
} else {
continue;
}
}
str = str.replaceAll(String.valueOf(c1), "") ;
System.out.println(c1 + "出现了" + count + "次");
}}
import java.util.*;
public class CharCount
{
public static void main(String[] args)
{
String s = "abcabcabcaaaaff";
Map<Character,Integer> m= new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
if(!m.containsKey(s.charAt(i))){
m.put(s.charAt(i),1);
}else{
int count = m.get(s.charAt(i));
m.put(s.charAt(i),count+1);
}
}
System.out.println(m);
}
}