Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中打印垂直直方图时出现格式错误_Java_Histogram - Fatal编程技术网

在java中打印垂直直方图时出现格式错误

在java中打印垂直直方图时出现格式错误,java,histogram,Java,Histogram,此代码 /** * <p>Title: Histogram</p> * * <p>Description: Histogram Drawing Class</p> * * <p>Copyright: Copyright (c) 2009</p> * * <p>Company: UMB</p> * * @author Bob Wilson * @version 03/04/2011

此代码

/**
 * <p>Title: Histogram</p>
 *
 * <p>Description: Histogram Drawing Class</p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: UMB</p>
 *
 * @author Bob Wilson
 * @version 03/04/2011
 */

public class Histogram 
{
  private int [] values;
  private int minIndex;
  private int maxIndex;
  private int maxLength;

  /** constructor for histogram drawing class
    * @param values: the array of integer values to draw
    * @param minIndex: the lowest index in the array for the range to draw
    * @param maxIndex: the highest index in the array for the range to draw
    * @param maxLength: the length of line to draw for the largest value
    */

  public Histogram(int [] values, int minIndex, int maxIndex, int maxLength) 
  {
    // initialize the values of the instance variables from the constructor parameters
    this.values = new int [maxIndex + 1];   // allocate memory for a copy of the values array
    this.minIndex = minIndex;
    this.maxIndex = maxIndex;
    this.maxLength = maxLength;

    // step 6: 
    // find largest number in values array for scaling length of bars

    int maxValue = values[0];  
    for ( int a = 1; a < values.length; a++){  
      if (values[a] > maxValue){  
        maxValue = values[a];  
      }  
    } 
    for ( int b = 1; b < values.length; b++) {
      values [b] = (values  [b] * maxLength) / maxValue;
    }

    // step 7:
    // copy data from values array to this.values array while scaling to maximum length of bars

    System.arraycopy(values,0,this.values,0,values.length);  


  }

  /** draw a horizontal bar graph
    */

  public void drawHor()
  {
      System.out.println("");

    // step 8:
    // draw horizontal bar graph (one line per roll value)

    for(int n = minIndex; n <= maxIndex; n++) {
      if (n < 10){
          System.out.print("Value " + n + ":  ");
      }
      else { 
          System.out.print("Value " + n + ": ");
      }

      for(int c = 1; c <= this.values[n]; c++) 
      {
        System.out.print("*"); 
      }
      System.out.println(" " + this.values[n]);       
    }
  }

  /** draw a vertical bar graph
    */

    public void drawVer()
    {
         System.out.println("");


    // step 9:
    // draw vertical bar graph (one column per roll value)
    for(int n = maxLength; n >= 1; n--)
    {

      if (n < 10){
          System.out.print("Count  " + n); 
      }
      else {
          System.out.print("Count " + n); 
      }

      for(int q = minIndex; q <= maxIndex; q++)
      {
        if (this.values[q] >= n)
        {
          System.out.print(" * ");
        }
        else
          System.out.print(" ");
      }
      System.out.println("");
    }
      System.out.println("Value:   2  3  4  5  6  7  8  9 10 11 12");
    }
}

/*201040*/
当它应该打印像这样更整洁的东西时

How many dice rolls do you want?
 [1000]
Estimated probabilities for outcome of the first roll:
Win:          0.214
Lose:         0.119
Roll again:   0.667

Value 2:  ******* 7
Value 3:  ********** 10
Value 4:  ************* 13
Value 5:  ************************** 26
Value 6:  ****************************** 30
Value 7:  ************************************ 36
Value 8:  *************************** 27
Value 9:  *********************** 23
Value 10: ******************* 19
Value 11: ********* 9
Value 12: ****** 6

Count 36                *
Count 35                *
Count 34                *
Count 33                *
Count 32                *
Count 31                *
Count 30             *  *
Count 29             *  *
Count 28             *  *
Count 27             *  *  *
Count 26          *  *  *  *
Count 25          *  *  *  *
Count 24          *  *  *  *
Count 23          *  *  *  *  *
Count 22          *  *  *  *  *
Count 21          *  *  *  *  *
Count 20          *  *  *  *  *
Count 19          *  *  *  *  *  *
Count 18          *  *  *  *  *  *
Count 17          *  *  *  *  *  *
Count 16          *  *  *  *  *  *
Count 15          *  *  *  *  *  *
Count 14          *  *  *  *  *  *
Count 13       *  *  *  *  *  *  *
Count 12       *  *  *  *  *  *  *
Count 11       *  *  *  *  *  *  *
Count 10    *  *  *  *  *  *  *  *
Count  9    *  *  *  *  *  *  *  *  *
Count  8    *  *  *  *  *  *  *  *  *
Count  7 *  *  *  *  *  *  *  *  *  *
Count  6 *  *  *  *  *  *  *  *  *  *  *
Count  5 *  *  *  *  *  *  *  *  *  *  *
Count  4 *  *  *  *  *  *  *  *  *  *  *
Count  3 *  *  *  *  *  *  *  *  *  *  *
Count  2 *  *  *  *  *  *  *  *  *  *  *
Count  1 *  *  *  *  *  *  *  *  *  *  *
Value:   2  3  4  5  6  7  8  9  10 11 12

我不明白为什么会发生这种情况,也不知道如何解决。任何意见都将不胜感激

您在
drawVert()
方法中的间距已关闭。有些代码很简单,但很容易修复,但您的代码有些马虎。虽然降低时间复杂度很好,但必须有正确的代码。通过间隔你的数字,你可以考虑一个更漂亮的x轴,通过重访你的恒星,你会发现当有命中和没有命中时,你没有相等的间隔。尝试查看输出和
drawVert()
方法

修改后的代码段:

    if (this.values[q] >= n)
    {
      System.out.print(" * "); //leading whitespace, asterisk, trailing whitespace = 3
    }
    else {
      System.out.print("   "); //whitespace, whitespace, whitespace = 3
    }
  System.out.println();

  System.out.print("Values: ");
  for(int i = minIndex; i < maxIndex; i++) {
    if(i < 10) {
      System.out.print(" " + i + " ");//leading and trailing whitespace for single digit vals
    } else {
      System.out.print(" " + i);//only leading for double digit vals
    }
  }
  System.out.println();
}
if(this.values[q]>=n)
{
System.out.print(“*”)//前导空格、星号、尾随空格=3
}
否则{
System.out.print(“”;//空白,空白,空白=3
}
System.out.println();
系统输出打印(“值:”);
对于(int i=minIndex;i
    if (this.values[q] >= n)
    {
      System.out.print(" * "); //leading whitespace, asterisk, trailing whitespace = 3
    }
    else {
      System.out.print("   "); //whitespace, whitespace, whitespace = 3
    }
  System.out.println();

  System.out.print("Values: ");
  for(int i = minIndex; i < maxIndex; i++) {
    if(i < 10) {
      System.out.print(" " + i + " ");//leading and trailing whitespace for single digit vals
    } else {
      System.out.print(" " + i);//only leading for double digit vals
    }
  }
  System.out.println();
}