停留在for循环中的第一个元素的百分比计算

时间:2016-02-25 07:10:51

标签: java foreach frequency

编辑:添加了显示频率计数的代码

这不是作业。我这样做是为了好玩。

我希望这段小代码足够,但我遇到了一个奇怪的问题。我计算了数组中每个数字的频率,并且工作正常。好吧,我想看看那些频率的百分比,所以我创建了一个小方法来计算它。

由于第一个元素的频率为42,而我的总字符数为1,553,因此每个元素的打印率为2.7%,而不是针对不同的频率进行更改。

我在调试过程中设置了断点,一旦频率计数器计算出第一个元素的频率,该值就会与其余元素一致。 O.o

我尝试在百分比计算之外放置一个for循环,它做了完全相同的事情。它从未脱离第一个频率计数器。

   System.out.println(" FREQUENCY COUNT RESULT");
    Collections.sort(array);

    Hashtable<Integer, Integer> table = new Hashtable<>();

    for (int i = 0; i < array.size(); i++) {
        if (table.containsKey(array.get(i))) {
            count = table.get(array.get(i));
            table.put(array.get(i), ++count);
        } else {
            table.put(array.get(i), 1);
        }
    }
    set = table.keySet();
    count = 0;
    for (Integer n : set) {
        count = table.get(n);
        System.out.println(n + ": Frequency - " + count);
    }
    System.out.println("TOTAL CHARACTERS IN ARRAY: " + countNonSpaces(array));
    System.out.println();
    System.out.println("PERCENTAGE REPORT");
    for(Integer n : set) {
        System.out.println(n + " Percentage: " + getPercentage(count, tally) + "%");
    }
}
public static double getPercentage(int count, int tally) {

    percentCounter = (count * 100.0) / tally;
    percentCounter = percentCounter * 100;
    percentCounter = Math.round(percentCounter);
    percentCounter = percentCounter / 100;
    return percentCounter;
}
public static int countNonSpaces(ArrayList<Integer> array) {

    for (int i = 0; i < array.size(); i++) {
        if (array.get(i) != ' ') {
            tally++;
        }
    }
    return tally;
}

除了百分比计数器之外,一切正常,所以这就是我将其他代码留下的原因。

这是频率输出的一部分:

    10: Frequency - 54
    9: Frequency - 33
    8: Frequency - 37
    7: Frequency - 42
    6: Frequency - 35
    5: Frequency - 36
    4: Frequency - 40
    3: Frequency - 50
    2: Frequency - 39
    1: Frequency - 42 //<---IT'S STUCK HERE...????
    TOTAL CHARACTERS IN ARRAY: 1553

这是相同元素及其(不正确的)百分比的输出:

    10 Percentage: 2.7%
    9 Percentage: 2.7%
    8 Percentage: 2.7%
    7 Percentage: 2.7%
    6 Percentage: 2.7%
    5 Percentage: 2.7%
    4 Percentage: 2.7%
    3 Percentage: 2.7%
    2 Percentage: 2.7%
    1 Percentage: 2.7%

3 个答案:

答案 0 :(得分:3)

您正在遍历集合,但未使用此集合中的任何值。

for(Integer n : set) {
    System.out.println(n + " Percentage: " + getPercentage(count) + "%");
}

很难从您的代码中判断出来,但如果该套装包含您的频率和您的点数,那么您可以计算它

for(SomeObj n : set) {
    System.out.println(" Percentage: " + getPercentage(n.tally, n.count) + "%");
}

基本上您需要计数和计数来计算百分比。每行的计数和计数都不同,因此需要将它们存储在您的集合中。

答案 1 :(得分:1)

您在打印循环中传递给getPercentage的count变量在循环期间的任何阶段都不会更新。

您需要为打印百分比的for循环添加计数,就像打印频率的for循环一样

for(Integer n : set) {
        count = table.get(n);  // add this line in the loop for printing percentage
        System.out.println(n + " Percentage: " + getPercentage(count, tally) + "%");
    }

答案 2 :(得分:0)

编辑添加了显示最常出现次数的代码

好吧,我睡觉了,今天早上再次尝试。我设置了断点以查看发生了什么,并且计数器坚持第一个元素,在这种情况下是整数39,计数为42次。每个百分比显示为42/1558的百分比(除了空格之外的所有字符的计数)。 由于百分比基本上与计数相同,只是除以计数,我完全摆脱了百分比方法,再次循环遍历元素,并确保在进入第二个之前重置计数器环。这是更改的代码(仅包括count和tally方法。另一种方法是getFileInfo(),它只从文本文件中获取数字并将它们放在一个数组中):

public static void getCount(ArrayList<Integer> array) {

    Hashtable<Integer, Integer> table = new Hashtable<>();

    for (int i = 0; i < array.size(); i++) {
        if (table.containsKey(array.get(i))) {
            count = table.get(array.get(i));
            table.put(array.get(i), ++count);
        } else {
            table.put(array.get(i), 1);
        }
    }
    set = table.keySet();
    count = 0;
    for (Integer n : set) {
        count = table.get(n);
        System.out.println(n + ": Frequency - " + count);
    }
    System.out.println();
    count = 0;
    System.out.println("PERCENTAGE REPORT");
    for(Integer n : set) {
        tally = 0; //<---Tally was being calculated twice. Had to reset.
        countNonSpaces(array); //get the tally once more after resetting
        count = table.get(n);
        double percentage = (count*100.0)/tally; //percentage is essentially "count" with an additional calculation so no new method needed
        percentage = percentage * 100;
        percentage = Math.round(percentage);
        percentage = percentage/100;
        System.out.println(n + ": Percentage - " + percentage + "%");
    }
    System.out.println();
    System.out.println("MOST SIGNIFICANT PERCENTAGES");
    count = 0;
    for(Integer n : set) {
        tally = 0;
        countNonSpaces(array);
        count = table.get(n);
        percentage = (count*100.0)/tally;
        percentage = percentage * 100;
        percentage = Math.round(percentage);
        percentage = percentage/100;

        // Picked a decent number (50), copied the code
        // from the frequency calculation, and added
        // a continue statement to only print the most frequent #'s.

            if(count < 50) { 
            continue;
        }
        System.out.println(n + ": Percentage - " + percentage + "%");
    }
}
public static int countNonSpaces(ArrayList<Integer> array) {

    for (int i = 0; i < array.size(); i++) {
        if (array.get(i) != ' ') {
            tally++;
        }
    }
    return tally;
}
}

为了简洁起见,我不会显示每个整数,但这是新的(且正确的)输出:

FREQUENCY COUNT RESULT
10: Frequency - 54
9: Frequency - 33
8: Frequency - 37
7: Frequency - 43
6: Frequency - 36
5: Frequency - 37
4: Frequency - 41
3: Frequency - 50
2: Frequency - 39
1: Frequency - 42

PERCENTAGE REPORT
10: Percentage - 3.47%
9: Percentage - 2.12%
8: Percentage - 2.37%
7: Percentage - 2.76%
6: Percentage - 2.31%
5: Percentage - 2.37%
4: Percentage - 2.63%
3: Percentage - 3.21%
2: Percentage - 2.5%
1: Percentage - 2.7%

TOTAL CHARACTERS IN ARRAY: 1558

MOST SIGNIFICANT PERCENTAGES
29: Percentage - 3.21%
15: Percentage - 3.59%
11: Percentage - 3.27%
10: Percentage - 3.47%
3: Percentage - 3.21%

感谢您帮助让我的齿轮转动。非常感谢。 : - )