Java-Arrays:如何找到大多数时候具有相似数字的数字?

Java-Arrays:如何找到大多数时候具有相似数字的数字?,java,arrays,Java,Arrays,我的一项任务有问题。 我被要求构建一个数组,用户将输入数组的长度。 最后,主要目标是打印任何数字中频率最大的数字。 例如,如果数组的长度为4,数字为:1000、212、1345、88818- 应该打印的数字是88818,因为它有更多相似的数字(数字8显示4次) 我确信我到目前为止所拥有的一切都是好的,但我发现该计划不完整有两个原因: 如果数组的大小大于2,则在创建数组后不会发生任何事情 如果其中一个数字有一个以上的数字,则不会发生任何情况 5个小时以来,我一直在想问题出在哪里,但还是没有什么好运

我的一项任务有问题。 我被要求构建一个数组,用户将输入数组的长度。 最后,主要目标是打印任何数字中频率最大的数字。 例如,如果数组的长度为4,数字为:1000、212、1345、88818- 应该打印的数字是88818,因为它有更多相似的数字(数字8显示4次)

我确信我到目前为止所拥有的一切都是好的,但我发现该计划不完整有两个原因:

  • 如果数组的大小大于2,则在创建数组后不会发生任何事情
  • 如果其中一个数字有一个以上的数字,则不会发生任何情况
  • 5个小时以来,我一直在想问题出在哪里,但还是没有什么好运气。 如果有人能帮助我,我将不胜感激

    我要指出的是,我不允许使用非int[]类型的数组,也不允许使用比数组更高级的任何材质

    以下是我到目前为止的情况:

        public static void frequentNumber() {
         int num=0;
        while (true) {
          System.out.println("Enter array size");
          num = in.nextInt();
          if (num >=1)
            break;
          }
        int[] a = createArray(num);
        int[] freq = new int[a.length]; //holds the frequency of each number in the primary array
        int count = 0, b;
        for (int i=0; i<a.length; i++) {
            b=a[i];
            while (b > 0) { // counts the number of digits 
                count++;
                b/=10;   }
            int[] k = new int[count]; // array of digits
            b=a[i];
            for (int j=0; j<k.length; j++) { // enters each digit into it's own box at the digits array
                k[j] = b % 10;
                b /= 10;   }
            int[] sortedK = new int[k.length];
            sortedK = sortArray(k); // sorts the array with bubble sort
            int max=0, maxNew=0;
            for (int s=1; s < sortedK.length; s++) {
                if (sortedK.length == 1) {
                    maxNew++;
                        break; }
                      else if (sortedK[s] != sortedK[s-1])
                    max=0;
                while (sortedK[s] == sortedK[s-1]) {
                    max++;
                    if (max > maxNew)
                        maxNew=max; 
                       }    
               }                           
            freq[i] = maxNew;     /* at this point, maxNew will have the amount of max frequent digits
                                       for the number at a[i]*/
        }                                   
        int maxIndex = checkMaxIndex(freq); // This Method returns an int that is the index of the highest frequency;
        printArray(a);
        System.out.println("The number with most freq digit is: " + a[maxIndex]);
    }
    
    public静态void frequentNumber(){
    int num=0;
    while(true){
    System.out.println(“输入数组大小”);
    num=in.nextInt();
    如果(num>=1)
    打破
    }
    int[]a=createArray(num);
    int[]freq=new int[a.length];//保存主数组中每个数字的频率
    整数计数=0,b;
    对于(inti=0;i0){//计算位数
    计数++;
    b/=10;}
    int[]k=new int[count];//数字数组
    b=a[i];
    
    对于(int j=0;j请考虑这一个指针,但我将从一个整数中的数字开始!

    /**
     * Take a decimal number as input, count the digits.
     * 
     * @param in
     *          A decimal number.
     * @return An array of 10 digit counts.
     */
    public static int[] digitCounter(int in) {
      int[] ret = new int[10];
      String v = String.valueOf(in);
      for (char c : v.toCharArray()) {
        // You should probably use a 'switch' here.
        if (c == '0') {
          ret[0]++;
        } else if (c == '1') {
          ret[1]++;
        } else if (c == '2') {
          ret[2]++;
        } else if (c == '3') {
          ret[3]++;
        } else if (c == '4') {
          ret[4]++;
        } else if (c == '5') {
          ret[5]++;
        } else if (c == '6') {
          ret[6]++;
        } else if (c == '7') {
          ret[7]++;
        } else if (c == '8') {
          ret[8]++;
        } else if (c == '9') {
          ret[9]++;
        }
      }
    
      return ret;
    }
    

    使用
    ret[c-'0']++
    避免“如果”s@vandale这是一个很好的提示。@Elliot Frisch谢谢你的建议。但我不明白你为什么要将数组的大小设置为10?这不是假设有10位数字吗?如果没有,如果你能解释给我就太好了。如果我的问题是愚蠢的,我才刚刚开始学习。vandale谢谢你的建议e提示。