Here is the code for my homework assignment. It's long overdue and I didn't do so well on it, but wanted to try to work on my mistakes and problems to actually do better in school; but anyways:
这是我的家庭作业的代码。它早就应该了,我在这方面做得并不好,但我想尝试解决我的错误和问题,以便在学校做得更好;但无论如何:
double x1, y1, x2, y2, x3, y3; // Three points for three vertices.
// (x1, y1), (x2, y2), (x3, y3)
Scanner input = new Scanner(System.in);
// The program asks the user what the name of the triangle is. Then the program
// asks the user for the cordinates of each point.
System.out.print("Enter a three letter name for the triangle: ");
String triangleName = input.next();
System.out.print("Coordinates of vertex " + triangleName.substring(0,1).toUpperCase() + ":");
x1 = input.nextDouble();
y1 = input.nextDouble();
System.out.print("Coordinates of vertex " + triangleName.substring(1,2).toUpperCase() + ":");
x2 = input.nextDouble();
y2 = input.nextDouble();
System.out.print("Coordinates of vertex " + triangleName.substring(2,3).toUpperCase() + ":");
x3 = input.nextDouble();
y3 = input.nextDouble();
double a, b, c; // These values will serve as the three side
// lengths between the points.
// The program displays side lengths in this block.
System.out.println("--- Side lengths ---");
a = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
System.out.printf(triangleName.substring(0,1).toUpperCase()
+ triangleName.substring(1,2).toUpperCase() + ":"
+ "%.3f", a);
System.out.println();
b = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2));
System.out.printf(triangleName.substring(1,2).toUpperCase()
+ triangleName.substring(2,3).toUpperCase() + ":"
+ "%.3f", b);
System.out.println();
c = Math.sqrt(Math.pow(x3-x1, 2) + Math.pow(y3-y1, 2));
System.out.printf(triangleName.substring(2,3).toUpperCase()
+ triangleName.substring(0,1).toUpperCase() + ":"
+ "%.3f", c);
System.out.println();
double perimeter, area, xCentroid, yCentroid, inRadius, inArea, semiPerimeter, triangleHeight, triangleBase;
// The program displays other measures in this block.
System.out.println("--- Other measures ---");
perimeter = a + b + c;
System.out.printf("Perimeter = " + "%.3f", perimeter);
System.out.println();
triangleHeight = y3 - 0.1;
triangleBase = x2 - x1;
area = (triangleHeight * triangleBase) / 2;
System.out.printf("Area = " + "%.3f", area);
System.out.println();
xCentroid = (x1 + x2 + x3) / 3;
yCentroid = (y1 + y2 + y3) / 3;
System.out.printf("Centroid = ( " + "%.3f", xCentroid + ", " + yCentroid + ")");
System.out.println();
semiPerimeter = 0.5 * perimeter;
inRadius = area / semiPerimeter;
System.out.format("Incircle radius = " + "%.3f", inRadius);
System.out.println();
inArea = Math.PI * Math.pow(inRadius, 2);
System.out.format("Incircle area = " + "%.3f", inArea);
System.out.println();
The problem in the code resides on the line for formatting the xCentroid
and yCentroid
values respectively. I am getting an error that says "java.util.IllegalFormatConversionException: f != java.lang.String"
. The math in the program runs fine and works too. I've removed the "%.3f"'s in the statement to see if I get the correct answers, which I do. It's just this format is pissing me off. Please respond, and help. I'll be up for awhile and will be patient.
代码中的问题存在于分别格式化xCentroid和yCentroid值的行上。我收到一条错误,上面写着“java.util.IllegalFormatConversionException:f!= java.lang.String”。程序中的数学运算正常并且也可以运行。我已经删除了声明中的“%。3f”,看看我是否得到了正确答案,我这样做了。只是这种格式让我感到沮丧。请回复,并提供帮助。我会好一会儿,耐心等待。
NEW CODE (FIXED) :
新代码(已修订):
System.out.print("Centroid = ");
System.out.print("(");
System.out.printf("%.3f", xCentroid);
System.out.print(", ");
System.out.printf("%.3f", yCentroid);
System.out.print(")");
System.out.println();
1 个解决方案
#1
3
In this code
在这段代码中
System.out.printf("Centroid = ( " + "%.3f", xCentroid + ", "
+ yCentroid + ")");
especially this
xCentroid + ", " + yCentroid + ")"
is not going to be a number, but a String
不会是一个数字,而是一个字符串
I guess what you want is
我猜你想要的是什么
System.out.printf("Centroid = (%.3f)", xCentroid + yCentroid);
#1
3
In this code
在这段代码中
System.out.printf("Centroid = ( " + "%.3f", xCentroid + ", "
+ yCentroid + ")");
especially this
xCentroid + ", " + yCentroid + ")"
is not going to be a number, but a String
不会是一个数字,而是一个字符串
I guess what you want is
我猜你想要的是什么
System.out.printf("Centroid = (%.3f)", xCentroid + yCentroid);