Java中System.out.print未按预期工作的三角形面积计算问题
解决Java三角形面积计算程序中输入提示顺序错误的问题
我能理解你第一次做编程作业遇到这种交互逻辑问题的困惑——程序先让你输入所有坐标,之后才弹出提示文本,这完全不符合正常的用户操作流程对吧?咱们来一步步拆解问题和解决方案:
原代码的核心问题
原代码犯了一个很典型的顺序错误:把读取输入的代码(scanner.nextDouble())放在了输出提示文本的代码(System.out.print())前面。程序执行时会先阻塞住,等你把6个坐标全输入完,才会执行输出提示的语句,这就导致提示文本在输入完成后才显示,完全失去了提示的作用。
另外还有个小bug:计算side2时的公式写错了,原代码用了(y2-y3),正确的应该是(y1-y3)(因为side2是顶点1到顶点3的距离)。
原代码
import java.util.Scanner; // Purpose: To get the area of a triangle public class ComputeTriangleArea { // main method public static void main(String[] args) { // Creating a scanner Scanner scanner = new Scanner(System.in); // Entering dimensions double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); double x3 = scanner.nextDouble(); double y3 = scanner.nextDouble(); // Inputting side 1 System.out.print("Enter the dimensions of side 1: "); double side1 = Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)); // Inputting side 2 System.out.print("Enter the dimensions of side 2: "); double side2 = Math.sqrt((x1-x3) * (x1-x3) + (y2-y3) * (y2-y3)); // Inputting side 3 System.out.print("Enter the dimensions of side 3: "); double side3 = Math.sqrt((x2-x3) * (x2-x3) + (y2-y3) * (y2-y3)); // Convert to Area double s = (side1+side2+side3) / 2; double area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3)); // Display the result System.out.printf("The area is %.2f\n", area); } }
原代码运行异常情况
输入
1 2 3 4 5 6后,输出为:java -cp . ComputeTriangleArea 1 2 3 4 5 6 Enter the dimensions of side 1: Enter the dimensions of side 2: Enter the dimensions of side 3: The area is 3.87 Exit code: 0
修正后的代码与说明
结合评论区的帮助,我调整了代码逻辑,同时优化了一些细节:
- 调整顺序:先输出输入提示,再读取对应的坐标值,让用户看到提示后再输入
- 修复公式错误:修正了
side2的计算逻辑 - 移除冗余代码:删掉了重复创建
Scanner的语句(一个Scanner实例就足够复用) - 优化提示文本:把“side”改成“vertex”,更符合输入坐标的实际场景
- 添加资源释放:最后关闭
Scanner,养成良好的编程习惯
import java.util.Scanner; // Purpose: To get the area of a triangle public class ComputeTriangleArea { // main method public static void main(String[] args) { // Creating a scanner Scanner scanner = new Scanner(System.in); // Inputting vertex 1 (先提示再读取) System.out.print("Enter the coordinates of vertex 1 (x y): "); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); // Inputting vertex 2 System.out.print("Enter the coordinates of vertex 2 (x y): "); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); // Inputting vertex 3 System.out.print("Enter the coordinates of vertex 3 (x y): "); double x3 = scanner.nextDouble(); double y3 = scanner.nextDouble(); // Calculate the lengths of each side double side1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); double side2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); double side3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); // Calculate area using Heron's formula double s = (side1 + side2 + side3) / 2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); // Display the result System.out.printf("The area is %.2f\n", area); // Close the scanner scanner.close(); } }
现在程序会按照正常逻辑运行:先提示输入第一个顶点坐标,等待你输入后再提示第二个、第三个,最后输出计算出的面积,完全符合预期的交互流程。
内容的提问来源于stack exchange,提问作者MrOutside
相关产品推荐
相关产品推荐

