【问题标题】:How to format my histogram?如何格式化我的直方图?
【发布时间】:2013-11-18 01:14:59
【问题描述】:

所以我真的很难格式化我的直方图,让它看起来像我们需要做的。我完全迷失和沮丧,因为我无法让它看起来像所要求的直方图。这是我们的说明的链接,那里有一张关于它应该是什么样子的图片,我不能在这里放图片,因为我没有足够的声誉,但这里是链接: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 );

}

}

【问题讨论】:

  • 你看到了什么输出?另外,你为什么要这样计算你的随机数?
  • 我的直方图间距太大,我的所有图都没有对齐。我以这种方式计算我的随机数,因为我需要一个给定范围之间的随机整数,因为我的最大整数和最小整数为 a & b。
  • 你的随机数计算是奇数,取一个1到100之间的随机数,很简单。至于您的直方图,不清楚您要绘制的直方图是什么?由于您的输出似乎是纯文本,请尝试包含一些实际输出以及您所期望的内容。
  • 该方法需要在任何范围内获取随机数,而不仅限于 1-100。我们的输出应该看起来像我在链接上的内容,我的只是格式不正确,这就是我感到困惑的地方。

标签: java arrays random integer histogram


【解决方案1】:

我不知道你的代码在做什么,所以我给你举个例子。假设你有 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
}

【讨论】:

  • 我的代码正在制作从给定范围生成的随机对的 num 次频率的直方图。给定的示例和解释在链接中。那里有一张关于它应该是什么样子的图片,但我的看起来有点像,但没有按照说明中的方式格式化
  • 不管你如何得到你的值,它们仍然会进入一个数组,对吗?而不是使用我的数组,只需将你的与我的其余代码一起使用,看看它是否是你正在寻找的输出。
  • 你想让我把你的代码放在哪里,我想在哪里调整东西,哪些方法?
  • 不要乱用你的代码。只需打开一个新的 java 文件并将我的编辑代码放在那里,再加上您必须获取数组的任何代码。然后测试一下。如果它看起来像您想要的那样,那么只需相应地修复您的代码。
  • 其实我没有看到你写完整的代码。您可以将我的方法放入您的文件中,然后从您的main 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多