第1个回答 2012-12-13
写了个最笨的方法,供你参考,这个思路比较简单,易于理解。
String string="12521278126351083178612332469";
int[] length_record=new int[10];
int length=string.length();
for(int i=0;i<length;i++){
String cur_char=string.charAt(i)+"";
try{
int char_value=Integer.parseInt(cur_char);
length_record[char_value]++;
}catch (Exception e) {
e.printStackTrace();
}
}
for(int i=0;i<10;i++){
System.out.println(length_record[i]);
}
第2个回答 2012-12-13
看你怎么做了,方法很多。
给你一个思路:定义一个数组int num[]=new int[10];循环将num[]数组里面的值全部赋值为0.
然后将字符串里面的数字利用字符串方法读出来,并且赋值给自己再次定义的一个整型int i;
然后做num[i]++;
最后利用循环将数组里面的东西全部输出,这样就得到了每个数字出现的次数。
可以继续追问。
第3个回答 2012-12-13
方法有很多种。比如:用CharAt方法逐个取出字符,用switch语句逐个对比,对应上了就计数加1,这很容易啊,自己试试嘛。
第4个回答 2012-12-13
public class Test {
public static void main(String[] arsg){
//对应数字的个数
int zero = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
String tempFlag = "12521278126351083178612332469";
for(int i = 0;i<tempFlag.length();i++){
flag = Integer.valueOf(tempFlag.substring(i, i+1));
switch(flag){
case 0: zero++;break;
case 1: one++;break;
case 2: two++;break;
case 3: three++;break;
case 4: four++;break;
case 5: five++;break;
case 6: six++;break;
case 7: seven++;break;
case 8: eight++;break;
case 9: nine++;break;
}
}
System.out.println(nine);
System.out.println(two);
}
第5个回答 2012-12-13
String str="23432454654655476572311";
Map<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<str.length();i++){
if(map.get(str.charAt(i))==null){
map.put(str.charAt(i), 1);
}else{
map.put(str.charAt(i), map.get(str.charAt(i))+1);
}
}
Set<Entry<Character,Integer>>set=map.entrySet();
Iterator<Entry<Character,Integer>> it=set.iterator();
while(it.hasNext()){
Entry<Character,Integer> entry=it.next();
System.out.println(entry.getKey()+"的个数是"+entry.getValue());
}
}