You will develop a program to count the number of times each number in the range is generated. Your program will generate 100 numbers in the range [0, 9] and you will print a histogram similar to the histogram shown below.
0 ********
1 ***********
2 ******
3 ****************
4 **************
5 *****
6 ************
7 ***********
8 ********
9 *********
To print the histogram shown above, your program must record the number of times each number is generated.
我试图将 num 和 count 传递到 print 方法中,然后将其传递到 switch 语句以将其打印为星号。但这给了我一个错误。建议?
这是我收到的错误:
required: int,int found: no arguments reason: actual and formal argument lists differ in length /tmp/java_9JsuaS/RandomSrv.java:55: error: method print in class RandomSrv cannot be applied to given types; 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[] 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
关于java - 方法/打印随机数的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031779/