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

时间:2014-11-17 23:31:32

标签: 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
 */

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?
10
Estimated probablities:
Win:20.0%
Lose:10.0%
Roll again:70.0%

Value 2:  ************ 12
Value 3:   0
Value 4:   0
Value 5:  ************************************ 36
Value 6:  ************ 12
Value 7:  ************************ 24
Value 8:  ************ 12
Value 9:  ************************ 24
Value 10:  0
Value 11:  0
Value 12:  0

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

什么时候应该像这样打印更整洁的东西

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

我无法弄清楚为什么会发生这种情况或如何解决这个问题。任何意见都将不胜感激!

1 个答案:

答案 0 :(得分:0)

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();
}