Scanner in = new Scanner(System.in);
String str_input = in.nextLine();
假如:输入的是 abjabab
则通过程序算出来 a3个 b3个 j1个
请问如何编写

解决方案 »

  1.   

    唉!
    [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]
      

  2.   

    [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]比楼上代码长啊,初级水平