所以我真的很难格式化直方图,使它看起来像我们要做的那样。我完全迷失和沮丧,因为我不能让它看起来像所要求的相同的直方图。这是我们指令的链接,那里有一张图片,看起来像什么,我不能在这里拍照,因为我没有足够的声誉,但这里是链接:http://www.cs.plu.edu/courses/csce144/fall2013/labs/lab07/RandomNumberTesting.html 在此先感谢您的帮助!
import java.util.Scanner; // to be able to read from the keyboard
public class RandomNum
{
/*
Method 1:
Find the maximun value in an array
*/
public static int max(int[]arr){
int maxValue = arr[0];
for ( int i=1; i < arr.length; i++ ){
if (arr[i] > maxValue){
maxValue = arr[i];
}
}
return maxValue;
}
/*
Method 2:
Compute a random integer in the range [a..b)
*/
public static int randomInteger(int a, int b){;
int randomNum;
randomNum = (int)(Math.random() * (b+1-a) + a);
return randomNum;
}
/*
Method 3:
Draw a Simple histogram of the array arr.
*/
public static void drawHistogram(int[] arr){
for ( int i=max(arr); i>0; i--){
System.out.print(i + "\t");
for (int j=0; j<arr.length; j++){
if ( arr[j] >= i){
System.out.print("x");
}else {
System.out.print(" ");
}if ( j%10 == 0 && j!=0){
System.out.print(" ");
}
}System.out.println();
}
for (int i=0; i<=arr.length; i++){
if ( i == 0){
System.out.print("\t\t");
}if ( i%10 == 0 && i != 0){
System.out.print(i + " ");
}
}System.out.println();
}
/*
Method 4:
Compute num random integers in the range [0..range) and put the frequency in arr[]
*/
public static void doSingleTest(int[] arr, int num, int range){
for (int i=1; i<=num; i++){
int random = randomInteger(0,range);
arr[random]++;
}
}
/*
Method 5:
Compute num pairs of random integers in the range [0..range) and put the frequency in arr[]
*/
public static void doPairsTest(int[] arr, int num, int range){
int rangeA = range/10;
int rangeB = range%10;
for (int i=1; i<=num; i++){
int random = ((randomInteger(0,rangeA) * 10) + randomInteger(0,rangeB));
arr[random]++;
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// declarations
int num;
// histogram presentation
System.out.println("Enter the amount of pairs you want to test: ");
num = keyboard.nextInt();
int[] arr = new int[100];
doPairsTest(arr, num, 99);
drawHistogram( arr );
}
}
答案 0 :(得分:0)
我不知道你的代码在做什么,所以我给你举个例子。假设你有20个值
int[] array = new int[20];
for (int i = 0; i < array.length; i++){
array[i] = (int)(Math.random() * 50 + 1); // random 1 to 50
}
首先我找到最大值;
int max = 0;
for(int i = 0; i < array.length; i++){
if (array[i] > max) {
max = array[i];
}
}
然后您可以使用max
值和array.length
来循环输出。
for (int i = max; i >= 0; i--){ // start at the max number to print out
// print i
System.out.printf("%-4f", i);
for (int j = 0; j < array.length; i++){ // remember, you're printing sideways
if (array[j] >= i) {
System.out.print("x");
} else {
System.out.print(" "); // if condition not met, print space
}
}
System.out.print("\n");
}
编辑:使用方法
public static void histogram(int[] array){
int max = 0;
for(int i = 0; i < array.length; i++){
if (array[i] > max) {
max = array[i];
}
}
for (int i = max; i >= 0; i--){ // start at the max number to print out
// print i
System.out.printf("%-4f", i);
for (int j = 0; j < array.length; i++){ // remember, you're printing sideways
if (array[j] >= i) {
System.out.print("x");
} else {
System.out.print(" "); // if condition not met, print space
}
}
System.out.print("\n");
}
}
调用main
方法
public static void main(String[] args){
// your code to get your array goes here.
int[] yourArray = // whatever your array is
histogram(yourArray);
// this is all you need to do, just call my method with your array
}