如何调整我在此代码中打印的数字?

时间:2012-07-01 22:22:02

标签: java

如何调整我在此代码中打印的数字? 由于代码中的数字是变量,我需要做一些与正常调整不同的事情吗?

 a = 4                                                                                                      ;
 b = 4                                                                                                      ;
 c = 1                                                                                                      ;
 x = 2                                                                                                      ;
 Root                      = (-1*b + Math.sqrt( Math.pow(b,2) - 4*a*c)/ 2*a                                );
 CoefficientOfXSquared     = (-(b*x+c)/(Math.pow(x,2))                                                     );
 CoefficientOfX            = (-(a*x+c)-c/x                                                                 );
 Constant                  = (-(a*Math.pow(x,2)+b*x)                                                       );
 System.out.println("\n\n\t    Given that: \n\t    CoefficientOfXSquared = " +a                            );
 System.out.println("\n\t    CoefficientOfX        = " +b                                                  );
 System.out.println("\n\t    Constant              = " +c                                                  );
 System.out.println("\n\t    Root                  = " +x                                                  );
 System.out.println("\n\n\t    x = " + Root                                                                );
 System.out.println("\n\t    a = " +  CoefficientOfXSquared                                                );
 System.out.println("\n\t    b = " +  CoefficientOfX                                                       );
 System.out.println("\n\t    c = " +  Constant                                                             );
 System.out.println("\n\n\n"                                                                               );

如果有人能解释如何正确调整,我将不胜感激。

3 个答案:

答案 0 :(得分:1)

我认为docs是最好的,显示如何使用printf()格式化数字输出这里也是formatting numbers in java

的精彩教程

答案 1 :(得分:1)

您可以这样做以正确对齐文本。

String.format("%50s", "Root = " + root);

尝试字符串格式功能,format arguments提供了许多自定义打印的选项。

答案 2 :(得分:1)

尝试使用字符串格式方法打印它们。像这样:

double x=1234.56;
double y=78.678;
System.out.printf("%n\t Root          =  %12.4f",  x);
System.out.printf("%n\t Constant      =  %12.4f%n",  y);

给出:

Root          =     1234.5600
Constant      =       78.6780

我假设这些是双打。查看printf了解更多信息。 崖