【问题标题】:methods/ print Histogram of random numbers方法/打印随机数的直方图
【发布时间】:2023-03-12 07:44:02
【问题描述】:

您将开发一个程序来计算范围内每个数字的生成次数。您的程序将在 [0, 9] 范围内生成 100 个数字,您将打印一个类似于下面显示的直方图的直方图。

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

要打印上面显示的直方图,您的程序必须记录每个数字生成的次数。

我正在尝试将 num 和 count 传递给 print 方法,然后通过 switch 语句将其传递为星号。但它给了我一个错误。建议?

这是我得到的错误:

必需:int,int 找到:无参数原因:实际和正式 参数列表长度不同 /tmp/java_9JsuaS/RandomSrv.java:55: 错误:类 RandomSrv 中的方法 print 不能应用于给定 类型; System.out.print("9" + this.print()); ^

import java.util.Random;

public class RandomSrv
{

    public void genNums(int total)
    {

        for(int counter = 0; counter < total; counter++) //counter for the random gen nums//

        {
            int UPPER_BOUND = 10;//max number it will go to: 9

            int count0 = 0; //stores counter//

            int count1 = 0;
            int count2 = 0;

            int count3 = 0;

            int count4 = 0;

            int count5 = 0;

            int count6 = 0;

            int count7 = 0;

            int count8 = 0;

            int count9 = 0;

            int count = 0;

            Random randObj = new Random(); //creating randoms object//
            int num = randObj.nextInt(UPPER_BOUND);//num ranges 1-10//
            print(num, count);
            switch (num) //counts it into catagories//
                {
                case 0: count = count0++;
                    System.out.print("0 " + this.print());
                    break;
                case 1: count = count1++;
                    System.out.print("1 " + this.print());
                    break;
                case 2: count = count2++;
                    System.out.print("2 " + this.print());
                    break;
                case 3: count = count3++;
                    System.out.print("3 " + this.print());
                    break;
                case 4: count = count4++;
                    System.out.print("4 " + this.print());
                    break;
                case 5: count = count5++;
                    System.out.print("5 " + this.print());
                    break;
                case 6: count = count6++;
                    System.out.print("6 " + this.print());
                    break;
                case 7: count = count7++;
                    System.out.print("7 " + this.print());
                    break;
                case 8: count = count8++;
                    System.out.print("8 " + this.print());
                    break;
                case 9: count = count9++;
                    System.out.print("9 " + this.print());
                    break;
                }

        }
    }
    public void print(int num, int count) //converting them to astericks//
    {
    for(int x = 0; x < count; x++)
        {
        System.out.println("*");
        }       
    }
}

【问题讨论】:

  • 代码太长,对想要的问题很复杂。我建议考虑数组,例如 int[10](从 0-9)数组中的每个位置都指的是它的值,例如:array[i++] 每次你有 i 值的数字时,i 位置被提升一。最后的打印将通过运行该数组来完成。
  • 它没有告诉你如何生成数字。您可以只 generate 4 each time 然后连续打印 100 个星号。

标签: java random


【解决方案1】:

这是一种方法:

int[] counts = new int[10];   // this array will hold the count for each number (0-9)
Random rand = new Random();   // the Random object

/* Loop 100 times getting a random number each time, and add to the corresponding count */
for(int i=0; i<100; i++)
{
    switch(rand.nextInt(10))
    {
        case 0: counts[0]++; break;
        case 1: counts[1]++; break;
        case 2: counts[2]++; break;
        case 3: counts[3]++; break;
        case 4: counts[4]++; break;
        case 5: counts[5]++; break;
        case 6: counts[6]++; break;
        case 7: counts[7]++; break;
        case 8: counts[8]++; break;
        case 9: counts[9]++; break;
        default: break;
    }
}

/* Loop 10 times printing the asterisks for each count */
for(int i=0; i<10; i++)
{
     System.out.print(i + " ");
     for(int j=0; j<counts[i]; j++)
         System.out.print("*");
     System.out.println();
}

在此处运行(在线编译器):http://rextester.com/UKSB96871

【讨论】:

  • 你可以写counts[rand.nextInt(10)]++而不是switch(并且可能将索引提取为var)。
【解决方案2】:

另一种解决方案

Map<Integer, String> groups = Stream.generate(() -> rand.nextInt(10))
                                    .limit(100)
                                    .collect(Collectors.groupingBy(Function.identity(),
                                                                   Collectors.mapping(e -> "*",
                                                                                      Collectors.joining())));

groups.forEach((number, hist) -> System.out.println(number + " " + hist));

【讨论】:

    【解决方案3】:

    我认为这是一个简单的方法:

    import java.util.Random;
    
    public class RandomSrv {
        private final int UPPER_BOUND;// max number it will go to
        private final int TOTAL;
        private final int[] numbers;
        private final int[] counter;
    
        public RandomSrv(int upperBound, int total) {
            this.UPPER_BOUND = upperBound;
            this.TOTAL = total;
            this.numbers = new int[this.TOTAL];
            this.counter = new int[this.UPPER_BOUND];
            this.genNums();
        }
    
        private void genNums() {
            Random randObj = new Random(); // creating random objects
            for (int counter = 0; counter < TOTAL; counter++) {
                // counter for the
                // random generate numbers
                int num = randObj.nextInt(UPPER_BOUND);
                this.numbers[counter] = num;
                this.counter[num]++;
            }
        }
    
        private void print(int count) // converting them to asterisks
        {
            System.out.print(String.format("%d: ", count));
            for (int x = 0; x < counter[count]; x++) {
                System.out.print("*");
            }
            System.out.println();
        }
    
        public void printAll() {
            for (int count = 0; count < this.UPPER_BOUND; count++) {
                this.print(count);
            }
        }
    
        public static void main(String[] commandArguments) {
            int upperBound = 10;
            int total = 100;
            new RandomSrv(upperBound, total).printAll();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 2013-07-23
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2014-05-17
      • 1970-01-01
      相关资源
      最近更新 更多