我需要打印如下的直方图:
My Histogram
12.0 #### 2.3
12.5 ### 1.55
13.0 ######################################## 21.9
13.5 ########################### 13.33
14.0 ################################## 17.25
14.5 ########## 5.21
我有要缩放的直方图。总的来说,标题将是第一个,并且位于第40位左下方的排名将是标度,即" nice"数字接近最大值。每个栏应该跟着它的长度表示。
这是我的代码。我需要帮助才能在直方图的右侧和左侧打印数字以打印然后标题。我有左边数字刻度所需的等式,但它不会打印。连同右边的标题和数据。
import java.util.Scanner;
public class Array3
{
public static void main(String[] arg)
{
Histogram h = new Histogram();
System.out.println(h.readHistogram());
} // end main
} // end Array
class Histogram
{
private String title;
private double start, stop;
private double[] data;
public Histogram(String title, double start, double stop, double[] data, int numBars)
{
this.title = title;
this.start = start;
this.stop = stop;
this.data = new double[numBars];
for(int i = 0; i < numBars; ++i)
{
if(data.length > i )
{
this.data[i] = data[i];
} // end if
} // end for
} // end constructor Histogram
public Histogram(){}
public String toString()
{
String numOfHash = "";
int a;
double max = data[0];
double step1 = (stop-start)/(data.length-1);
for(int k = 0; k < data.length; ++k)
{
if(data[k] > max)
{
max = data[k];
}
}
for(int i = 0; i < data.length; ++i)
{
a = (int) (data[i] + .5);
double startNum = start + step1*i;
System.out.println(startNum);
for(int j = 0; j < a * 40/max ; ++j)
{
numOfHash = numOfHash + "#";
} // end for loop
numOfHash = numOfHash + "\n";
} // end for loop
return numOfHash;
} // end toString
public Histogram readHistogram()
{
int num = 0;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a histogram.");
System.out.println("Title: ");
String title = stdin.nextLine();
System.out.println("Start value: ");
double start = stdin.nextDouble();
System.out.println("End Value: ");
double stop = stdin.nextDouble();
System.out.println("Data: ");
double[] data = new double[6];
for(int i = 0; i < 6; ++i)
{
data[i] = stdin.nextDouble();
num++;
}
return new Histogram(title, start, stop, data, num);
}
} //结束直方图
答案 0 :(得分:1)
你的toString方法需要一些工作。首先,您需要找到最大值。接下来,您可以计算缩放系数。现在,您可以绘制直方图。
这是代码。
import java.util.Scanner;
public class Array3 {
public static void main(String[] arg) {
Histogram h = new Histogram();
System.out.println(h.readHistogram());
} // end main
} // end Array3
class Histogram {
private String title;
private double start, stop;
private double[] data;
public Histogram(String title, double start, double stop, double[] data,
int numBars) {
this.title = title;
this.start = start;
this.stop = stop;
this.data = new double[numBars];
for (int i = 0; i < numBars; ++i) {
if (data.length > i) {
this.data[i] = data[i];
} // end if
} // end for
} // end constructor Histogram
public Histogram() {
}
public String toString() {
String numOfHash = title + "\n";
double max = data[0];
for (int k = 1; k < data.length; ++k) {
max = Math.max(max, data[k]);
}
double scaleFactor = max / 40D;
double a = start;
for (int i = 0; i < data.length; ++i) {
numOfHash += String.format("%.1f", a) + " ";
int count = (int) Math.round(data[i] / scaleFactor);
for (int j = 0; j < count; ++j) {
numOfHash += "#";
} // end for loop
numOfHash += " " + String.format("%.2f", data[i]) + "\n";
a += 0.5D;
} // end for loop
return numOfHash;
} // end toString
public Histogram readHistogram() {
int num = 0;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a histogram.");
System.out.println("Title: ");
String title = stdin.nextLine();
System.out.println("Start value: ");
double start = stdin.nextDouble();
System.out.println("End Value: ");
double stop = stdin.nextDouble();
System.out.println("Data: ");
double[] data = new double[5];
for (int i = 0; i < 5; ++i) {
data[i] = stdin.nextDouble();
num++;
}
stdin.close();
return new Histogram(title, start, stop, data, num);
}
} // end Histogram