这是对以下问题的重新提问:How to tackle a homework prgram that reads three sides for a triangle and computes the area if the input is valid?
这又是一项任务:
创建一个名为MyTriangle的类,它包含以下两个方法:
/** 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)
编写一个测试程序,读取三角形的三个边,并在输入有效时计算该区域。否则,它会显示输入无效。
尝试下面:问题:我无法弄明白这一点,不断重读这一章并没有突破任何一面墙。 问题在以下代码中发表了评论:
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
作为该地区的答案。我做错了什么?
我已经超过7个小时,因为我不了解可能存在的问题。我进入CompSci课程已经2个月了。
答案 0 :(得分:2)
如果三角形有效,您的方法isValid
将返回true,但您假设相反:
double a = area(side1, side2, side3);
boolean isV = isValid(side1, side2, side3);
if (isV)
System.out.println("Inout is Invalid");
边栏11.1
,22.2
和33.3
的输入数据(在评论中)进一步支持了这一点。那是不是三角形,它是一系列重叠的线段。
如果您输入完全有效的3/4/5
三角形,则会收到错误消息,指出它不是三角形。
所以,只需将上面的代码更改为(无效数据上无法调用area
):
if (! isValid(side1, side2, side3))
System.out.println("Inout is Invalid");
else
System.out.println("Area is " + area(side1, side2, side3));
进行这些更改:
package test2;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides, separated by spaces: ");
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
if (isValid(s1, s2, s3))
System.out.println("Area is: " + area(s1, s2, s3));
else
System.out.println("Input is Invalid");
}
public static boolean isValid(double s1, double s2, double s3) {
return ((s1 + s2 > s3) && (s1 + s3 > s2) && (s2 + s3 > s1));
}
public static double area(double s1, double s2, double s3) {
double s = (s1 + s2 + s3) / 2;
double theArea = Math.pow(s * (s - s1) * (s - s2) * (s - s3), 0.5);
return theArea;
}
}
并输入有效三角形,例如3/4/5
或5/5/8
,可为您提供正确的结果:
Enter three sides, separated by spaces: 3 4 5
Area is: 6.0
Enter three sides, separated by spaces: 5 5 8
Area is: 12.0
答案 1 :(得分:1)
if (isV)
应为if (!isV)
。如果条件不正确,那么您只能 执行Heron的公式,其中包含可能导致s * (s - side1) * (s - side2) * (s - side3)
为负的潜在危险输入。使用Java基元类型未定义取负数的平方根。
答案 2 :(得分:1)
使用此..苍鹭的公式
double sidesSvalue = (a + b + c)/2.0d;
double productofSides = (sidesSvalue * (sidesSvalue -a) * (sidesSvalue -b) * (sidesSvalue -c));
double Area= Math.sqrt(productofSides );
return Area;
只要您的值无效,他们就会进入该区域,请更正您应使用的条件代码
if(!isValid)
print error message
else
calculate area
使用DMAS操作时,最后一件事总是尝试使用相同的原始类型。