0

这是我即将到来的大学实习的代码:

import java.util.Random;

public class Practical4_Assessed 
{

public static void main(String[] args) 
{

    Random numberGenerator = new Random ();
    int[] arrayOfGenerator = new int[100];
    int[] countOfArray     = new int[10];
    int count;

    for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++)
    {
        count = numberGenerator.nextInt(10);
        countOfArray[count]++;
        arrayOfGenerator[countOfGenerator] = count + 1;
    }

    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
    {
        if (countOfNumbersOnLine == 10)
        {
            System.out.println("");
            countOfNumbersOnLine = 0;
            countOfOutput--;
        }
        else
        {
            if (arrayOfGenerator[countOfOutput] == 10)
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                countOfNumbersOnLine++;
            }
            else
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                countOfNumbersOnLine++;
            }
        }
    }

    System.out.println("");
    System.out.println("");

    String occurrencesReport = "";
    String graph = "";

    for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
    {
        occurrencesReport += "The number " + (countOfNumbers + 1) + 
            " occurs " + countOfArray[countOfNumbers] + " times.";

        if (countOfNumbers != 9)
            graph += (countOfNumbers + 1) + "   ";
        else
            graph += (countOfNumbers + 1) + "  ";

        for (int a = 0; a < countOfArray[countOfNumbers]; a++)
        {
            graph += "*";
        }
        occurrencesReport += "\n";
        graph += "\n";
    }

    System.out.println(occurrencesReport);
    System.out.println(graph);

    int max = 0;
    int test = 0;
    for (int counter = 0; counter < countOfArray.length; counter++)
    {
        if (countOfArray[counter] >= max)
        {
            max = countOfArray[counter];
            test = counter + 1;
        }
    }

    System.out.println("The number that appears the most is " + test + ".");

}
}

该程序创建一个数组,该数组将存储 100 个整数(所有整数都在 1 到 10 之间),这些整数由随机数生成器生成,然后每行打印出该数组的 10 个数字。然后它扫描这些整数,计算每个数字出现的频率,并将结果存储在第二个数组中。在此之后,它会输出一个星号水平条形图,显示每个数字出现的频率,然后最终输出出现频率最高的数字。

我以为我已经完全完成了代码,但我刚刚意识到如果多个数字出现相同的次数,我的代码的最后一部分将无法处理这个问题,例如如果数字 3 和 5 都出现 12有时,代码只能产生其中之一。

有没有人有办法解决这个问题?

谢谢,安德鲁

4

3 回答 3

0

我假设这不是一些家庭作业,所以我为您提供了另一种方法。

-使用CollectionlikeArrayList而不是Array.

-使用方法Collections.frequency(Object o)来了解该值出现的次数Collection

于 2012-10-28T18:24:48.133 回答
0

而不是仅仅做

System.out.println("The number that appears the most is " + test + ".");

再次循环countOfArray,为每个与 具有相同值的元素执行打印max

于 2012-10-28T18:36:52.437 回答
0

有几种方法可以解决这个问题,从快速到复杂。最简单的方法是像这样暴力破解它:

int max = 0;
//int test = 0;
for (int counter = 0; counter < countOfArray.length; counter++)
{
    if (countOfArray[counter] >= max)
    {
        max = countOfArray[counter];
        //test = counter + 1;
    }
}

System.out.print("The number that appears the most is");
boolean first = true;
for(int i = 0; i < countOfArray.length; i++)
{
    if(countOfArray[i] == max)
    {
        if(first)
            first = false;
        else
            System.out.print(",");
        System.out.print(" " + (i+1) );
    }
}
System.out.println(".");

这是输出:

6   2   6   5   6   8   9   3   5   8   
9   8   10  10  4   5   8   9   8   5   
1   7   8   5   6   7   10  4   5   4   
2   7   9   2   3   3   1   2   10  3   
5   2   10  1   1   6   3   3   8   10  
2   6   10  2   5   1   4   10  8   7   
7   8   7   3   7   8   3   4   5   5   
7   8   9   8   6   6   8   1   10  3   
2   5   4   6   9   9   10  10  1   10  
9   4   10  9   7   3   4   3   2   4   

The number 1 occurs 7 times.
The number 2 occurs 9 times.
The number 3 occurs 11 times.
The number 4 occurs 9 times.
The number 5 occurs 11 times.
The number 6 occurs 9 times.
The number 7 occurs 9 times.
The number 8 occurs 13 times.
The number 9 occurs 9 times.
The number 10 occurs 13 times.

1   *******
2   *********
3   ***********
4   *********
5   ***********
6   *********
7   *********
8   *************
9   *********
10  *************

The number that appears the most is 8, 10.

有很多更干净的方法可以解决它,但希望这会给你一个不错的开始!

于 2012-10-28T18:37:12.163 回答