[英]How can I make the numbers that I print in this code right adjusted?
How can I make the numbers that I print in this code right adjusted? 如何调整我在此代码中打印的数字? Since the numbers in the code are variables do I need to do something different than normally adjusting it? 由于代码中的数字是变量,我需要做一些与正常调整不同的事情吗?
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" );
I would appreciate it if someone could explain how to make it right adjusted. 如果有人能解释如何正确调整,我将不胜感激。
我认为文档是最好的,显示如何使用printf()
格式化数字输出这里也是一个关于java中格式化数字的好教程
You can do this to right justify the text. 您可以这样做以正确对齐文本。
String.format("%50s", "Root = " + root);
Try String format function, format arguments provide many options to customize the printing. 尝试String格式函数, 格式参数提供了许多自定义打印的选项。
Try using string format method to print them. 尝试使用字符串格式方法打印它们。 Like this: 像这样:
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);
which gives: 这使:
Root = 1234.5600
Constant = 78.6780
I am assuming that these are doubles. 我假设这些是双打。 Look up printf for more info. 查看printf了解更多信息。 Cliff 悬崖
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.