2012-07-01 47 views
1

如何調整我在此代碼中打印的數字? 由於代碼中的數字是變量,我是否需要做一些不同於通常的調整?如何調整我在此代碼中打印的數字?

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"                    ); 

我將不勝感激,如果有人能解釋如何使其正確調整。

回答

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獲取更多信息。 懸崖

1

一個偉大的教程你可以做到這一點,以右對齊文本。

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

嘗試字符串格式化功能,format arguments提供了許多選項來定製打印。