簡體   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