[英]How to format my histogram?
因此,在格式化直方图以使其看起来像我们需要做的事情时,我确实很难。 我完全迷失了自己,感到沮丧,因为我无法使它看起来像所要求的直方图一样。 这是我们的说明的链接,上面有一张图片,看起来像是什么样子,我无法在这里填写图片,因为我没有足够的声誉,但这是链接: 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 );
}
}
我不知道您的代码在做什么,所以我举一个例子。 假设您有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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.