使用数组统计数字中每个数字的出现次数

5

我需要编写一个使用数组的程序,它接受一个数字并返回该数字中每个数字出现的次数。我觉得我可能在这里过于复杂化了。

import java.util.*;
class Exercice7 {

  public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Veuillez saisir un nombre naturel:");   // Get number

    int n = sc.nextInt();                                       // Store number as n

    String str = Integer.toString(n);                           // Store n as string

    int length = str.length();                                    // Store string length as length

    int arr[] = new int[length];                                // Declare array with as many elements as n has digits

    int digit[] = {0,1,2,3,4,5,6,7,8,9};                        // Declare array with the digits to look for

    int count = 0;                                                // Number of occurences of each digit

    for (int i=(length-1); i>=0; i--) {                         // Fill array with digits from number input
        while (n>0) {
            arr[i]= n%10;
            n = n/10;
        }
    }

    for (int j=0; j<10; j++) {
        count = 0;
        for (int i=0; i<length; i++) {
            if (arr[i]==digit[j]) {
                count++;
            }
        }
        if (count>0) {
        System.out.println(digit[j] + " occurs " + count + " times.");
        }
    }
  }
}

这段代码只返回0和1的数量,而且还是错误的。能否有人指导一下正确的方向?
3个回答

4

声明一个包含十个元素([0..9])的数组 - 在这里,您将拥有数字中每个数字出现的次数。只需使用counts [3]即可获得数字3的出现次数。

然后,您只需迭代字符串数字并将下一个字符读取为整数并增加计数器。这样,您只有一个循环。例如,在您的数字中有3时,您使用counts [3] ++


不确定这是否是您所想要的,但它似乎可以工作! - ktouchie
很高兴你成功了 - 这就是为什么我没有提供任何代码的原因。当你自己回答(难?)问题时,这会更好、更有趣。干得好,即使我的方法有点不同 :) - deem
谢谢,deem!我完全同意。 :) - ktouchie

0
你可以尝试将其转换为字符串并逐个读取每个字符。
int counts[] = {0,0,0,0,0,0,0,0,0,0};  
int myNumber=123222;//example
String string=""+myNumber; //converting integer to String

for(int i=0;i<string.length();i++){
 try{
   int n=Integer.parseInt(string.charAt(i)+"")
   counts[n]=counts[n]+1;
 }catch(Exception e){}

}

然后将其打印出来:

for (int i=0; i<counts.length; i++) {
    System.out.println(i + " occurs " + counts[i] + " times.");
}

计数数组在初始化时应该所有元素都为0,因为这些值代表每个数字的计数,而不是数字本身(数字本身由索引表示)。 - Ciara

0

感谢deem的回答。虽然我还不是完全理解你的意思,但是这有助于我找对了方向:

import java.util.*;
class Exercice7 {

public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Veuillez saisir un nombre naturel:"); //* Get number */
    int num = sc.nextInt(); //* Store number as n */
    String str = Integer.toString(num); //* Store n as string *//

    char digit[] = {'0','1','2','3','4','5','6','7','8','9'};
    int count = 0;

    for (int i=0; i<10; i++) {
        for (int j=0; j<(str.length()); j++) {
            if (str.charAt(j) == digit[i]) {
                count++;
            }
        }
        if (count>0) {
            System.out.println(digit[i] + " apparait " + count + " fois.");
            count = 0;
        }            
    }
}
}

可能不是最简单的方法,但它能够正常工作!如果您想改进代码,请随意添加更多注释。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接