使用数组计算出现次数 - Java

时间:2015-02-12 05:44:39

标签: java arrays

我迷失了如何将0到9之间的100个随机生成的数字与数组值进行比较,也在0-9之间,然后打印结果。对我来说很容易,我是编码的新手,我知道我很糟糕。我觉得好像我75%那里。我知道有一些方法可以减少一些代码的冗余,但是我似乎很难用这些技术。

这是我到目前为止所拥有的:

public static void main(String[] args) {
    double randomNum = 0;
    for (int i = 0; i < 100; i++) {
        randomNum = Math.random() * 10;

    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
    int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (double j = 0; j <arrayNums.length; j++){
        if (arrayNums[0] == randomNum) {
            count0++;
        }
        else if (arrayNums[1] == randomNum){
            count1++;
        }
        else if (arrayNums[2] == randomNum){
            count2++;
        }else if (arrayNums[3] == randomNum){
            count3++;
        }else if (arrayNums[4] == randomNum){
            count4++;
        }else if (arrayNums[5] == randomNum){
            count5++;
        }else if (arrayNums[6] == randomNum){
            count6++;
        }else if (arrayNums[7] == randomNum){
            count7++;
        }else if (arrayNums[8] == randomNum){
            count8++;
        }
        else{
            count9++;
        }

    }
    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
    }
}

}

感谢任何和所有帮助。

5 个答案:

答案 0 :(得分:2)

  • 使用HashMap作为计数器比保持单独使用更优雅 每个项目的变量。
  • 使用Random.nextInt(10)生成0到9(含)之间的随机数。

        Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
        Random rand = new Random();
        for (int i = 0; i < 100; i++) {
            int randomNum = rand.nextInt(10);
            Integer currentCount = counter.get(randomNum);
            if (null == currentCount) {
                counter.put(randomNum, 1);
            } else {
                counter.put(randomNum, currentCount+1);
            }
        }
        for (Integer key : counter.keySet()) {
            System.out.println(key + " -> " + counter.get(key));
        }
    

示例输出

0 -> 12
1 -> 10
2 -> 9
3 -> 6
4 -> 8
5 -> 9
6 -> 11
7 -> 12
8 -> 14
9 -> 9

答案 1 :(得分:1)

首先将所有值添加到array。然后迭代数组并将值作为键存储在HashMap中,并将值存储为出现次数。然后在迭代之后,您将获得您想要存档的内容。

例如:

 int[] arr = {0, 1, 0, 2, 5, 2, 4, 6, 0, 4, 7, 8, 9, 0, 2, 5, 7, 6};
 Map<Integer, Integer> countMap = new HashMap<>();
  for (int i : arr) {
    Integer currentCount = countMap.get(i);
    if (currentCount != null) { 
       countMap.put(i, currentCount + 1);
    } else {
       countMap.put(i, 1);
    }
  }
 for(Map.Entry<Integer,Integer> entry:countMap.entrySet()){
        System.out.println("Number of occurrences of "+entry.getKey()
                                                   +" : "+entry.getValue());
 }

答案 2 :(得分:1)

好吧,而不是使用

int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;

您可以使用:

int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

您的方法将如下所示:

  public static void main(String[] args) {
        int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int[] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < 100; i++) {
            int randomNum = (int) (Math.random() * 10);
            for (int j = 0; j < 10; j++) {
                if (arrayNums[j] == randomNum) {
                    count[j]++;
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.println("Occurrences of " + i + ": " + count[i]);
        }
    }

答案 3 :(得分:0)

你以错误的方式调用随机数。它应该是这样的:

Random rnd = new Random();
int randomNum = rnd.nextInt(10);

你也把计数变量放错了。那么你的完整代码将是这样的:

public static void main(String[] args) {
    Random random = new Random();
    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
    for (int i = 0; i < 100; i++) {
        int randomNum = random.nextInt(10);


        int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (double j = 0; j <arrayNums.length; j++){
          if (arrayNums[0] == randomNum) {
            count0++;
          }
          else if (arrayNums[1] == randomNum){
            count1++;
          }
          else if (arrayNums[2] == randomNum){
            count2++;
          }else if (arrayNums[3] == randomNum){
            count3++;
          }else if (arrayNums[4] == randomNum){
            count4++;
          }else if (arrayNums[5] == randomNum){
            count5++;
          }else if (arrayNums[6] == randomNum){
            count6++;
          }else if (arrayNums[7] == randomNum){
            count7++;
          }else if (arrayNums[8] == randomNum){
            count8++;
          }
          else{
            count9++;
          }

        }

    }
    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
}

答案 4 :(得分:0)

您在for循环的每次迭代中将计数器重置为0。我不知道第二个循环是什么。

    public static void main(String[] args) {
    double randomNum = 0;

    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;

    for (int i = 0; i < 100; i++) {
        randomNum = Math.random() * 10;

        switch(randomNum) {
            case 0: count0++; break;
            case 1: count1++; break;
            case 2: count2++; break;
            case 3: count3++; break;
            case 4: count4++; break;
            case 5: count5++; break;
            case 6: count6++; break;
            case 7: count7++; break;
            case 8: count8++; break;
            case 9: count9++; break;
        }

    }


    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
}