繁体   English   中英

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

[英]Counting Occurrences With An Array - Java

我迷失了如何比较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);
    }
}

}

任何和所有的帮助表示赞赏。

  • 使用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

首先将所有值添加到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());
 }

好吧,而不是使用

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

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

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

您将在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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM