这是我的任务:
Create a class named MyTriangle that contains the following two methods:
/** Return true if the sum of any two sides is * greater than the third side. */ public static boolean isValid (double side1, double side2, double side3) /** Return the area of the triangle. */ public static double area (double side1, double side2, double side3)
Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid.
下面的尝试: 问题:我无法弄清楚这一点,不断重读这一章并没有突破任何障碍。 问题在以下代码中进行了评论:
import java.util.Scanner;
public class NewClass1 {
double area;
double side1, side2, side3;
double x1, x2, x3, y1, y2, y3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two integers for side 1:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter two integers for side 2:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
System.out.print("Enter two integers for side 3:");
double x3 = input.nextDouble();
double y3 = input.nextDouble();
boolean isValid = true;
if (isValid) {
System.out.println("Input is invalid");
}
else
area(side1, side2, side3); //Using area does not work and I don't know how to remedy this. I've read the chapter over and over... I cannot get it to work.
}
public static double area(double side1, double side2, double side3) {
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
double x3 = 0;
double y3 = 0;
side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (area);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
}
查看代码,有人可以解释我做错了什么,并解释可能的补救措施。一切都在那里,我就是无法连接点。
修订--代码
import java.util.Scanner;
public class NewClass1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter side 1: ");
double side1 = input.nextDouble();
System.out.print("Enter side 2: ");
double side2 = input.nextDouble();
System.out.print("Enter side 3: ");
double side3 = input.nextDouble();
double a = area(side1, side2, side3);
boolean isV = isValid(side1, side2, side3);
if (isV)
System.out.println("Inout is Invalid");
else
System.out.println("Area is: " + a);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
public static double area(double side1, double side2, double side3) {
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double theArea = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (theArea);
}
}
我不断收到 NaN
作为该区域的答案。我做错了什么?
最佳答案
所以你写下: 如果输入有效,则显示错误消息。否则(else)计算区域。 你应该只交换你的 if 和 else 部分!如果您使用有效三角形的点调用它,您的程序永远不会调用 area() 方法。 此外,您永远不会调用 isValid() 方法。您将 true 赋值给变量,然后在下一行检查它,但实际检查它的方法从未被调用。
您还需要 isValid() 的辅助变量,但您只能在 area() 方法中计算它们。你应该在得到分数后立即计算它们。
关于java - 如果输入有效,如何处理读取三角形三边并计算面积的家庭作业程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953391/