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

时间:2022-08-25 21:34:02

I need to write a program using arrays, which takes a number and returns the number of incidents of each digit within that number. I think I may be overcomplicating things here.

我需要使用数组编写一个程序,它接受一个数字并返回该数字中每个数字的事件数。我想我可能在这里过于复杂。

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.");
        }
    }
  }
}

This code only returns the number of 0s and 1s and it's wrong anyway. Could someone push me in the right direction?

这段代码只返回0和1的数字,无论如何都是错的。有人能把我推向正确的方向吗?

3 个解决方案

#1


4  

Declare array with ten elements ([0..9]) - there you will have occurences of each digit in your number. Simply using counts[3] will get you number of occurences of digit 3.

声明具有十个元素的数组([0..9]) - 在那里您将出现数字中每个数字的出现。只需使用计数[3]就可以得到数字3的出现次数。

Then you just iterate over string number and read next char as integer and increase counter. This way you have only one loop. For example, having 3 in your number, you use counts[3]++.

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

#2


0  

you can try converting to String and read each char

你可以尝试转换为String并读取每个char

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){}

}

then to print it out:

然后把它打印出来:

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

#3


0  

Thanks, deem, for your answer. I didn't completely understand what you meant, but it helped me get on the right track:

谢谢,认为,你的答案。我并不完全理解你的意思,但它帮助我走上正轨:

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;
        }            
    }
}
}

It may not be the easiest way, but it works! Feel free to add more comments if you want to make improvements to the code.

它可能不是最简单的方法,但它有效!如果要对代码进行改进,请随意添加更多注释。

#1


4  

Declare array with ten elements ([0..9]) - there you will have occurences of each digit in your number. Simply using counts[3] will get you number of occurences of digit 3.

声明具有十个元素的数组([0..9]) - 在那里您将出现数字中每个数字的出现。只需使用计数[3]就可以得到数字3的出现次数。

Then you just iterate over string number and read next char as integer and increase counter. This way you have only one loop. For example, having 3 in your number, you use counts[3]++.

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

#2


0  

you can try converting to String and read each char

你可以尝试转换为String并读取每个char

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){}

}

then to print it out:

然后把它打印出来:

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

#3


0  

Thanks, deem, for your answer. I didn't completely understand what you meant, but it helped me get on the right track:

谢谢,认为,你的答案。我并不完全理解你的意思,但它帮助我走上正轨:

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;
        }            
    }
}
}

It may not be the easiest way, but it works! Feel free to add more comments if you want to make improvements to the code.

它可能不是最简单的方法,但它有效!如果要对代码进行改进,请随意添加更多注释。