如果这样的问题的答案已经存在,我深表歉意,我是一个非常新手的编码员,我可能无法找到它。
我正在开发一个项目,根据三个点的 x 和 y 值给出距离、周长和面积。使用 printf 命令,应该将这些点四舍五入到千分位。
它应该看起来像这样:
三角形={(8.500 ,1.250) ,(2.500 ,1.250) ,(2.500 ,9.250)}
然而,我只能设法得到:
三角形 = { ( 8.5001.2502.5001.2502.5009.250
这是我的代码:
public class TriangleCalculator
{
public static void main(String[] args)
{
System.out.println("Enter the x- and y- coordinates of the first point");
Scanner keyBoard = new Scanner (System.in);
double pointOneX = keyBoard.nextDouble();
double pointOneY = keyBoard.nextDouble();
System.out.println("Enter the x- and y- coordinates of the second point");
double pointTwoX = keyBoard.nextDouble();
double pointTwoY = keyBoard.nextDouble();
System.out.println("Enter the x- and y- coordinates of the third point");
double pointThreeX = keyBoard.nextDouble();
double pointThreeY = keyBoard.nextDouble();
System.out.printf("Triangle = { ( %.3f", pointOneX);
System.out.printf("%.3f", pointOneY);
System.out.printf("%.3f", pointTwoX);
System.out.printf("%.3f", pointTwoY);
System.out.printf("%.3f", pointThreeX);
System.out.printf("%.3f", pointThreeY);
}
}
如果有人可以告诉或展示如何正确编写 printf 语句或如何正确对齐我的代码,我将不胜感激。
最佳答案
您的代码不会自行添加括号。
尝试:
System.out.printf("%.3f", pointOneY);
System.out.printf("),(");
System.out.printf("%.3f", pointTwoX);
System.out.printf("%.3f", pointTwoY);
System.out.printf("),(");
System.out.printf("%.3f", pointThreeX);
System.out.printf("%.3f", pointThreeY);
System.out.printf(")}");
关于java - 对齐数字时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370906/