Java 方法/打印随机数直方图

Java 方法/打印随机数直方图,java,random,Java,Random,您将开发一个程序来计算范围内每个数字生成的次数。您的程序将生成范围为[0,9]的100个数字,并且您将打印一个类似于下图所示直方图的直方图 要打印上面显示的直方图,程序必须记录每个数字生成的次数 我试图将num和count传递到print方法中,然后通过switch语句将其打印为星号。但这给了我一个错误。建议 以下是我得到的错误: 必需:int,int found:无参数原因:实际和正式 参数列表长度不同/tmp/java_9JsuaS/RandomSrv.java:55: 错误:类Random

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

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

我试图将num和count传递到print方法中,然后通过switch语句将其打印为星号。但这给了我一个错误。建议

以下是我得到的错误:

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

import java.util.Random;
公共类随机变量
{
公用事业单位(整数合计)
{
for(int counter=0;counter
这里有一种方法:

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();
}
int[]计数=新的int[10];//此数组将保存每个数字(0-9)的计数
Random rand=新的Random();//随机对象
/*循环100次,每次获取一个随机数,并添加到相应的计数*/

对于(int i=0;i,这里有一种方法:

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();
}
int[]counts=new int[10];//此数组将保存每个数字(0-9)的计数
Random rand=new Random();//随机对象
/*循环100次,每次获取一个随机数,并添加到相应的计数*/

对于(int i=0;i我认为这是一个简单的方法:

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();
    }
}
import java.util.Random;
公共类随机变量{
private final int上限;//它将到达的最大值
私人总决赛;
私人最终整数[]编号;
专用最终int[]计数器;
公共随机SRV(整数上限,整数总计){
this.UPPER_BOUND=上限;
这个.总计=总计;
this.numbers=新整数[this.TOTAL];
this.counter=newint[this.UPPER_-BOUND];
this.genNums();
}
私有void genNums(){
Random randObj=new Random();//创建随机对象
用于(整数计数器=0;计数器<总计;计数器++){
//柜台
//随机生成数
int num=randObj.nextInt(上界);
此.numbers[计数器]=num;
这个。计数器[num]++;
}
}
私有void print(int count)//将它们转换为星号
{
系统输出打印(字符串格式(“%d:”,计数));
对于(int x=0;x<计数器[计数];x++){
系统输出打印(“*”);
}
System.out.println();
}
public void printAll(){
for(int count=0;count
我认为这是一个简单的方法:

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();
    }
}
import java.util.Random;
公共类随机变量{
private final int上限;//它将到达的最大值
私人总决赛;
私人最终整数[]编号;
专用最终int[]计数器;
公共随机SRV(整数上限,整数总计){
this.UPPER_BOUND=上限;
这个.总计=总计;
this.numbers=新整数[this.TOTAL];
this.counter=newint[this.UPPER_-BOUND];
this.genNums();
}
私有void genNums(){
Random randObj=new Random();//创建随机对象
用于(整数计数器=0;计数器<总计;计数器++){
//柜台
//随机生成数
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));