小编Jea*_*ean的帖子

使用数组作为计数器

我正在尝试创建一个模块,计算每个数字在给定数字中出现的次数.我遇到的问题是,不是将相应数字的数组值加1,而是添加10,或者它连接数组的默认值(在这种情况下为0),尽管这似乎不太可能.

我的模块:

public class UtilNumber{
    public static int [] Occurence(int nb){
        int temp;
        int [] t = new int [10];

        while (nb !=0){
            temp = nb % 10;
            for (int i = 0; i < t.length ; i++){
                t[temp]++;
            }
            nb /= 10;
        }
        return t;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的主要:

import java.util.scanner;

public class Primary{
    public static void main(String [] args){
        Scanner keyboard = new Scanner(System.in);
        int [] tab;
        int nb = keyboard.nextInt();
        tab = UtilNumber.Occurence(nb);
        for (int i = 0 …
Run Code Online (Sandbox Code Playgroud)

java arrays

0
推荐指数
1
解决办法
138
查看次数

标签 统计

arrays ×1

java ×1