为什么这个Java格式化的输出不是正确的? [英] Why is this Java formatted output not right-justified?

查看:179
本文介绍了为什么这个Java格式化的输出不是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我正在尝试格式化的输出。

我试图产生一个小数表:



<$对于(int j = 0; j <8; j ++){
row = 0),(int i = 0; i <8; i ++){
> (双)5 *我;
column =(double)j + 1;
System.out.format(%6.3f,row / column);
System.out.print(\t);
}
System.out.format(%n);



$ b $ p


$ b pre> 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
5.000 2.500 1.667 1.250 1.000 0.833 0.714 0.625
10.000 5.000 3.333 2.500 2.000 1.667 1.429 1.250
15.000 7.500 5.000 3.750 3.000 2.500 2.143 1.875
20.000 10.000 6.667 5.000 4.000 3.333 2.857 2.500
25.000 12.500 8.333 6.250 5.000 4.167 3.571 3.125
30.000 15.000 10.000 7.500 6.000 5.000 4.286 3.750
35.000 17.500 11.667 8.750 7.000 5.833 5.000 4.375

API表示如果不抛出' - '标志,输出将会是正确的,这是我想要的。我在%之后加上了'',表示我希望在必要时用空格填充输出。我缺少什么?



(我没有使用作业标签,因为我没有注册任何课程。)



  

System.out.format(%6.3f,row / column);

应该是:

  System.out.format(%6.3f,row / column); 

您得到:

  0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
5.000 2.500 1.667 1.250 1.000 0.833 0.714 0.625
10.000 5.000 3.333 2.500 2.000 1.667 1.429 1.250
15.000 7.500 5.000 3.750 3.000 2.500 2.143 1.875
20.000 10.000 6.667 5.000 4.000 3.333 2.857 2.500
25.000 12.500 8.333 6.250 5.000 4.167 3.571 3.125
30.000 15.000 10.000 7.500 6.000 5.000 4.286 3.750
35.000 17.500 11.667 8.750 7.000 5.833 5.000 4 .375


I'm a beginner, and I'm experimenting with formatted output.
I'm trying to produce a table of decimals:

for (int i = 0; i < 8; i++) {
  for (int j = 0; j < 8; j++) {
    row = (double) 5*i;
    column = (double) j + 1;
    System.out.format("% 6.3f", row/column);
    System.out.print("\t");
  }
  System.out.format("%n");
}

This produces:

 0.000   0.000    0.000   0.000   0.000   0.000   0.000   0.000 
 5.000   2.500    1.667   1.250   1.000   0.833   0.714   0.625 
 10.000 5.000    3.333   2.500   2.000   1.667   1.429   1.250  
 15.000 7.500    5.000   3.750   3.000   2.500   2.143   1.875  
 20.000 10.000   6.667   5.000   4.000   3.333   2.857   2.500  
 25.000 12.500   8.333   6.250   5.000   4.167   3.571   3.125  
 30.000 15.000   10.000  7.500   6.000   5.000   4.286   3.750  
 35.000 17.500   11.667  8.750   7.000   5.833   5.000   4.375  

The API says that if the '-' flag is not thrown, then the output will be right-justified, which is what I want. I have put ' ' after % to indicate that I want the output padded with spaces when necessary. What am I missing?

(I haven't used the homework tag, because I am not enrolled in any course.)

解决方案

You have added a space incorrectly into the formatting String:

System.out.format("% 6.3f", row/column);

Should be:

System.out.format("%6.3f", row/column);

And you get:

 0.000         0.000         0.000         0.000         0.000         0.000         0.000         0.000        
 5.000         2.500         1.667         1.250         1.000         0.833         0.714         0.625        
10.000         5.000         3.333         2.500         2.000         1.667         1.429         1.250        
15.000         7.500         5.000         3.750         3.000         2.500         2.143         1.875        
20.000        10.000         6.667         5.000         4.000         3.333         2.857         2.500        
25.000        12.500         8.333         6.250         5.000         4.167         3.571         3.125        
30.000        15.000        10.000         7.500         6.000         5.000         4.286         3.750        
35.000        17.500        11.667         8.750         7.000         5.833         5.000         4.375  

这篇关于为什么这个Java格式化的输出不是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆