求出输入的字符串中相同的字符的个数 Scanner in = new Scanner(System.in);String str_input = in.nextLine();假如:输入的是 abjabab则通过程序算出来 a3个 b3个 j1个请问如何编写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 唉![code=Jav]public static void main(String[] args) { // TODO Auto-generated method stub String str = "abjabab"; Map<Character, Integer> map = new HashMap<Character, Integer>(); for(int i=0;i<str.length();i++){ char c = str.charAt(i); int count = map.containsKey(c)?map.get(c):0; count++; map.put(c, count); } }[/code] [code=Jav]public static void main(String[] args) { String str = "abjabab"; char[] c = str.toCharArray(); char[] c_type = new char[c.length]; int[] c_count = new int[c.length]; int n = 0; for(int i=0;i<c.length;i++){ boolean isIn = false; for(int j=0;j<n;j++){ if(c[i] == c_type[j]){ c_count[j]++; isIn = true; } } if(!isIn){ c_type[n] = c[i]; c_count[n]++; n++; } } for(int i=0;i<n;i++){ System.out.println(c_type[i]+":"+c_count[i]); } }[/code]比楼上代码长啊,初级水平 FTP下载文件问题 javamail 开发遇到的一个奇怪问题 如何在JOptionPane中加载JPanel对象(完整显示) 如何制作java更新包 如何把文件内容读取到一个String 并且修改后又回写到这个文件 初级菜鸟问题 请问线程不用stop那用什么停止? 参数传递的问题!(大家讨论讨论) 谁有关于读取数据库中所有表的名字、字段名的例子? 菜鸟问题? java基本语法疑问,new int[5];为何不合法? javamail发信后收不到邮件
[code=Jav]
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abjabab";
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
int count = map.containsKey(c)?map.get(c):0;
count++;
map.put(c, count);
}
}
[/code]
String str = "abjabab";
char[] c = str.toCharArray();
char[] c_type = new char[c.length];
int[] c_count = new int[c.length];
int n = 0;
for(int i=0;i<c.length;i++){
boolean isIn = false;
for(int j=0;j<n;j++){
if(c[i] == c_type[j]){
c_count[j]++;
isIn = true;
}
}
if(!isIn){
c_type[n] = c[i];
c_count[n]++;
n++;
}
}
for(int i=0;i<n;i++){
System.out.println(c_type[i]+":"+c_count[i]);
}
}[/code]比楼上代码长啊,初级水平