如何格式化我的直方图? [英] How to format my histogram?
问题描述
所以我真的有在格式化我直方图使它看起来像它正在为我们做需要一个困难时期。我完全失去了和沮丧,因为我不能使它看起来像一样直方图有什么要求。这里是链接到我们的指令,有一个画面上有什么是假设的样子,我不能坑了这里拍照,因为我没有足够的声誉,但这里是链接:http://www.cs.plu.edu/courses/csce144/fall2013/labs/lab07/RandomNumberTesting.html
在此先感谢您的帮助!
So i'm really having a hard time in formatting my histogram to make it look like what it is required for us to do. I am totally lost and frustrated because I just can't make it look like the same histogram as what is ask for. Here is the link to our instruction and there is a picture there on what it is suppose to look like, I can't pit up pictures here because I don't have enough reputation but here is the link: http://www.cs.plu.edu/courses/csce144/fall2013/labs/lab07/RandomNumberTesting.html Thanks in advance for the help!
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 );
}
}
推荐答案
我不知道你的code是干什么的,让我给你举个例子。假设你有20个值
I don't know what your code is doing, so I'll give you an example. Say you have 20 values
int[] array = new int[20];
for (int i = 0; i < array.length; i++){
array[i] = (int)(Math.random() * 50 + 1); // random 1 to 50
}
首先,我会找到的最大值;
First I'd find the max value;
int max = 0;
for(int i = 0; i < array.length; i++){
if (array[i] > max) {
max = array[i];
}
}
然后就可以使用最大
值和 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");
}
}
在电话主
法
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
}
这篇关于如何格式化我的直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!