class TestCountString {
    public static void main(String[] args) {
    String a = "zhhuu";
    System.out.println("你输入的是:"+a+";长度为:"+a.length());
    Makeout(a);
  }
  
  static void Makeout(String s) {
    Character[] d = new Character[s.length()];
    int sum = 0;
    int z = 0;
    int count = 0;
    Character[] c = new Character[s.length()];
    for(int i=0;i<s.length();i++) {
    c[i] = s.charAt(i);
    }
    
    for(int k=0;k<s.length();k++) {
    System.out.print(c[k]);
    }
    System.out.println();
    
    for(int u=0;u<s.length();u++) {
    
    d[u] = c[u];                      //循环把c[]的值赋予d[];
    for(int y=0;y<s.length();y++) {
    if(d[u]==c[y]) {
    z = y;
    System.out.println("在第"+(z+1)+"个位置");
    count++;
    sum++;
    }    
  }
    System.out.println(d[u]+"有"+sum+"个");
    System.out.println();
    sum = 0;
    
    
    /*if(count==s.length()) {
    System.exit(-1);
    }*/  
  }
 }
}

解决方案 »

  1.   

    错误:c[i] = s.charAt(i); 应该是c[i] = new Character(s.charAt(i)); 运行错误的原因是你对Character对象比较的时候用的==而不是equals,不if(d[u]==c[y]) 改成if(d[u].equals(c[y]))即可.另外没有必要用Character对象,直接用char就好了,那个可以用==判断.
    还有你的Character[] d 和 int z没有必要,循环用i j k 不要用x y
    由于不知道你的最终目的是啥,只对一些写法进行了修改,输出结果和你现在的逻辑(改为equals)是一样的,.  static void Makeout(String s) {
        if(s==null) return ;
        
        int sum = 0;
        int count = 0;
        
        char targetChar;
        char[] charSequence = s.toCharArray();//把s转为char序列
        
        System.out.println(Arrays.toString(charSequence));//打印字符数组快捷的方式    for (int i = 0; i < charSequence.length; i++) {
          
          targetChar = charSequence[i];
          
          for (int j = 0; j < charSequence.length; j++) {
            if (targetChar == charSequence[j]) {          System.out.println("在第" + (j + 1) + "的位置");
              count++;
              sum++;
            }
          }
          System.out.println(targetChar + "有" + sum + "个");
          System.out.println();
          sum = 0;      /*if(count==s.length()) { 
           System.exit(-1); 
           }*/
        }
      }
      

  2.   

    不是错  是没有达到效果  字符串为"zhhuu"  
    应该是输出:在第一个位置 
    有1个z
     
    在第2个位置 
    在第3个位置
    有2个h 在第4个位置
    在第5个位置
    有2个u     -----------到此结束了可是现在输出了2次h和u的位置  变成 在第一个位置 
    有1个z在第2个位置 
    在第3个位置
    有2个h 在第2个位置 
    在第3个位置
    有2个h     ------------这个h输出了2次  请帮我纠正让只输出一次意思就是找到h后下面的h就不要在找了 
      

  3.   

      static void Makeout(String s) {
        if(s==null) return ;
        
        int sum = 0;
        int count = 0;
        
        char targetChar;
        char[] charSequence = s.toCharArray();//把s转为char序列
        boolean[] hasCompare = new boolean[charSequence.length];//用来记录某个位置上的字符是否已经比较过了
        
        System.out.println(Arrays.toString(charSequence));//打印字符数组快捷的方式    for (int i = 0; i < charSequence.length; i++) {
          
          if(!hasCompare[i]){
            targetChar = charSequence[i];
            
            for (int j = i; j < charSequence.length; j++) {
              if (targetChar == charSequence[j]) {
                hasCompare[j] = true;
                System.out.println("在第" + (j + 1) + "的位置");
                count++;
                sum++;
              }
            }
            System.out.println(targetChar + "有" + sum + "个");
            System.out.println();
            sum = 0;
          }
          /*if(count==s.length()) { 
           System.exit(-1); 
           }*/
        }
      }
      

  4.   

    boolean[] hasCompare = new boolean[charSequence.length];//用来记录某个位置上的字符是否已经比较过了 
    学习!