从数组值制作非图形直方图

发布于 2024-09-06 12:56:16 字数 1076 浏览 4 评论 0 原文

通过 starPrint 方法,我需要使数组中填充的每个数字的频率显示在直方图中,如下所示:

1=3***
2=4****
3=7*******

等等。它需要填充的星星数量等于该数字出现的频率!目前我正在获取数组长度的星号数量。

public static void main(String[] args) {

    int matrix[][] = new int[100][2];

    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length; column++) {
            matrix[row][column] = (int) (Math.random() * 6 + 1);
        }

    }
    int[] hist1 = frequency(matrix);

    String star = starPrint(hist1);
    for (int i = 1; i < hist1.length; i++) {
        System.out.print(" \n" + hist1[i] + star);
    }

}

public static String starPrint(int[] value) {

    String star = "";
    for (int i = 0; i < value.length; i++) {

        star += "*";
    }
    return star;
}

public static int[] frequency(int[][] matrix) {

    int[] nums = new int[7];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            nums[matrix[i][j]] += 1;
        }
    }
    return nums;
}

Via the starPrint method I need to make the frequency of each number populated in the array display in a histogram as such:

1=3***
2=4****
3=7*******

and so on. It needs the number of stars populated that are equal to the frequency of the number appearing! At the moment I'm getting the number of asterisks of the length of the array.

public static void main(String[] args) {

    int matrix[][] = new int[100][2];

    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length; column++) {
            matrix[row][column] = (int) (Math.random() * 6 + 1);
        }

    }
    int[] hist1 = frequency(matrix);

    String star = starPrint(hist1);
    for (int i = 1; i < hist1.length; i++) {
        System.out.print(" \n" + hist1[i] + star);
    }

}

public static String starPrint(int[] value) {

    String star = "";
    for (int i = 0; i < value.length; i++) {

        star += "*";
    }
    return star;
}

public static int[] frequency(int[][] matrix) {

    int[] nums = new int[7];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            nums[matrix[i][j]] += 1;
        }
    }
    return nums;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-09-13 12:56:16

首先,明星应该会改变吧?那么

String star = starPrint(hist1);

应该在这里

for (int i = 1; i < hist1.length; i++) {
        System.out.print(" \n" + hist1[i] + star);
}

第二个你的 starPrint 方法将不得不改变(除非这是家庭作业中所述的方法???)从 到

public static String starPrint(int[] value) {

public static String starPrint(int value) {

意味着你将需要随机获得的值而不是数组的长度

for (int i = 0; i < value; i++) { 

Not value.length

First thing, stars should be changing right ? then

String star = starPrint(hist1);

should be within here

for (int i = 1; i < hist1.length; i++) {
        System.out.print(" \n" + hist1[i] + star);
}

Second your starPrint method will have to change (unless that is how method is stated in the homework ???) from

public static String starPrint(int[] value) {

to

public static String starPrint(int value) {

which means that you will need the value that you got at random and not the length of the array

for (int i = 0; i < value; i++) { 

Not value.length

我早已燃尽 2024-09-13 12:56:16

这是 示例。 org/wiki/Ada_%28programming_language%29" rel="nofollow noreferrer">Ada 可能会指导您。

Max_Count  : constant Integer := 1_200;
Bin_Size   : constant Integer := 100;
--
type Histogram is array (0 .. Max_Count / Bin_Size - 1) of Integer;
Graph : Histogram := (others => 0);
--
for J in Graph'Range loop --'
   TIO.Put(Label(J));
   for K in 1 .. (Graph(J) * Plot_Size) / Game_Count loop
      TIO.Put("*");
   end loop;
   TIO.New_Line;
end loop;

附录:请注意,starPrint() 始终返回相同数量的星星。每次打印 hist1[i] 的值时,都会打印出那么多的星星。

附录:考虑将 starPrint(int[] value) 更改为 starPrint(int value)

Here's an example in Ada that may guide you.

Max_Count  : constant Integer := 1_200;
Bin_Size   : constant Integer := 100;
--
type Histogram is array (0 .. Max_Count / Bin_Size - 1) of Integer;
Graph : Histogram := (others => 0);
--
for J in Graph'Range loop --'
   TIO.Put(Label(J));
   for K in 1 .. (Graph(J) * Plot_Size) / Game_Count loop
      TIO.Put("*");
   end loop;
   TIO.New_Line;
end loop;

Addendum: Note that starPrint() always returns the same number of stars. Each time you print the value of hist1[i], print out that many stars.

Addendum: Consider changing starPrint(int[] value) to starPrint(int value).

埋情葬爱 2024-09-13 12:56:16

您是否考虑过使用 Map?您可以迭代该数组,并针对每个数字检查它当前是否是地图的键。如果是,则获取关联值并递增它。如果没有,请将数字以及迄今为止发生的次数(一)放入地图中。

然后,在打印直方图时,只需迭代地图的 keySet() 即可获取值。

Have you considered using a Map<Integer, Integer>? You could iterate through the array, and for each number check to see if it was currently a key for the map. If so, get the associated value and increment it. If not, put the number in the map, along with the number of times it has occurred so far (one).

Then when it come to printing the histogram, just iterate through the keySet() of the map, and get the values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文