所以这是我关于代码的第一份家庭作业,过去的几个小时我一直在努力弄清楚这一点,但是我完全感到沮丧。
我试图通过输入三角形的三个边来查找三角形的区域(设法弄清楚了该怎么做),但是当我实际编译并运行代码时,.print无法正常工作
到目前为止,这是我的代码:
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
输入侧面1的尺寸,输入侧面2的尺寸,输入侧面3的尺寸,面积为3.87
退出代码:0
有人会介意以正确的方向指导我吗?
非常感谢您!
编辑:这是我在以下注释中从所有人的帮助下得到的代码:
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 side 1
scanner = new Scanner(System.in);
System.out.print("Enter the dimensions of side 1: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
// Inputting side 2
System.out.print("Enter the dimensions of side 2: ");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
// Inputting side 3
System.out.print("Enter the dimensions of side 3: ");
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
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));
// 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);
}
}
答案 0 :(得分:0)
您可以尝试以下代码:
// main method
public static void main(String[] args) {
// Creating a scanner
Scanner scanner = new Scanner(System.in);
// Inputting side 1
System.out.print("Enter the dimensions of side 1: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
// Inputting side 2
System.out.print("Enter the dimensions of side 2: ");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
// Inputting side 3
System.out.print("Enter the dimensions of side 3: ");
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
double side1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double side2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y2 - y3) * (y2 - y3));
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);
}
已更新
使用单个Scanner
而不是多个create Scanner
,enter
键不会破坏System.in
(这是我以前的错误观点,认为enter
键会破坏{ {1}})
答案 1 :(得分:0)
该输出是您编写的代码所期望的。 我假设您希望输出为(请提供预期的输出)
Enter the dimensions of side 1: 1 2
Enter the dimensions of side 2: 3 4
Enter the dimensions of side 3: 5 6
The area is 3.87
如果您观察代码,则首先扫描了所有6个数字,然后打印语句。这样输出就如您所愿。
如果您希望输出如我所显示的那样,则需要打印每一面的对帐单,在每个打印对帐单后扫描2个数字。