0

直方图

--------------------------------------------------------
  1 ****(4)
  2 ******(6)
  3 ***********(11)
  4 *****************(17)
  5 **************************(26)
  6 *************************(25)
  7 *******(7)
  8 ***(3)
  9 (0)
 10 *(1)
--------------------------------------------------------

基本上以上是我的 prgram 需要做的.. 我在某个地方遗漏了一些东西,任何帮助都会很棒:)

import java.util.Random; 
public class Histogram
{

    /*This is a program to generate random number histogram between
    1 and 100 and generate a table */

    public static void main(String args[])
    {

        int [] randarray = new int [80];
        Random random = new Random();
        System.out.println("Histogram");
        System.out.println("---------");

        int i ;
        for ( i = 0; i<randarray.length;i++)
        {   
            int temp = random.nextInt(100); //random numbers up to number value 100
            randarray[i] = temp;

        }

        int [] histo = new int [10];
        for ( i = 0; i<10; i++)
        {
            /* %03d\t, this generates the random numbers to
            three decimal places so the numbers are generated
            with a full number or number with 00's or one 0*/


            if (randarray[i] <= 10) {
                histo[i] = histo[i] + 1;
            //System.out.println("*");
            }
            else if ( randarray[i] <= 20){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 30){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <= 40){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 50){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=60){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=70){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=80){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=90){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=100){
            histo[i] = histo[i] + 1;
            }

            switch (randarray[i])
            {
            case 1: System.out.print("0-10 | "); break;
            case 2: System.out.print("11-20 | "); break;
            case 3: System.out.print("21-30 | "); break;
            case 4: System.out.print("31-40 | "); break;
            case 5: System.out.print("41-50 | "); break;
            case 6: System.out.print("51-60 | "); break;
            case 7: System.out.print("61-70 | "); break;
            case 8: System.out.print("71-80 | "); break;
            case 9: System.out.print("81-90 | "); break;
            case 10: System.out.print("91-100 | "); 
            }
                for (int i = 0; i < 80; i++)
            {
              randomNumber = random.nextInt(100)
              index = (randomNumber - 1) / 2;
              histo[index]++;
            }
    }
   }
 }
4

5 回答 5

1

您的随机数据包含 80 个值,但您只遍历前 10 个值。您应该遍历所有 80 个值。您将使用 histo[1]、histo[2] 等而不是 histo[i]。

此外,整个大开关块可以简化为

histo[randarray[i] / 10]++;

而不是创建 randarray 然后循环遍历它,您可以简单地执行以下操作:

for(int i = 0; i < 80; i++)
{
    histo[random.nextInt(100) / 10]++;
}
于 2009-11-18T21:03:56.940 回答
0

如果我没看错,那么我认为你的switch陈述是混乱的。

您的randarray价值观来自,0 < randarray[i] < 100但您只10为您的switch. 那可能会丢掉一些东西。

只是我的猜测。

于 2009-11-18T20:59:14.300 回答
0

尽管数组中有 80 个随机数,但您只循环了 10 次。

这条线也有缺陷。您不想使用相同的计数器变量来遍历数组并确定要增加哪个直方图 bin。

 if (randarray[i] <= 10) {
                        histo[i] = histo[i] + 1;

更新:

您应该尝试提出一种算法将随机值转换为 bin,因为您的解决方案不可扩展,并且您将习惯于不良的编程习惯。

于 2009-11-18T21:02:49.260 回答
0

您的最后一个 for 循环不会编译 - 那里有很多语法错误。

你需要用你的随机数填充历史,然后打印出来。

您还想在中断之前在每个 case 语句中打印出 hist 数组中的计数。

如果你继续学习编程,祝你好运!

于 2009-11-18T21:05:14.270 回答
0

要考虑的另一点是,您的直方图将是平坦、均匀的分布,而不是像您在问题中显示的正态分布。

于 2009-11-18T21:21:22.687 回答