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

标签 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

我不明白为什么会发生这种情况或如何解决它。任何意见将不胜感激!

最佳答案

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

关于java - 在java中打印垂直直方图时出现格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26983944/

相关文章:

algorithm - 如何计算分布式系统中大量数据的分布(直方图)?

java - 设计复杂系统的两种方法 : Top-down vs Bottom-up

java - java中JSONArray无法转换为JSONObject

java - 如何在抽屉导航中添加菜单项运行时?

java - 对 JBoss web.xml 的更改无效

c++ - 与已知图像匹配的局部二进制模式

python - 大型数组的 Numpy 直方图

python - 在同一图上绘制多个直方图的问题

java - RandomAccessFile 类如何使用 randomAccessFile.read() 方法返回字节;

r - 强制R将直方图绘制为概率(相对频率)