如果输入有效,如何处理读取三角形三边并计算面积的家庭作业程序? (2)
How to tackle a homework prgram that reads three sides for a triangle and computes the area if the input is valid? (2)
这是对以下问题的重新提问:
又是任务:
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
作为该区域的答案。我做错了什么?
我已经处理了 7 个多小时,只是因为我不了解其中可能存在的问题。我学习这门 CompSci 课程已经 2 个月了。
你的方法 isValid
returns 如果三角形有效但你假设相反:
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
if (isV)
应该是 if (!isV)
。如果条件不正确,那么您 仅 执行带有潜在危险输入的 Heron 公式,可能导致 s * (s - side1) * (s - side2) * (s - side3)
为负数。 Java 基本类型未定义负数的平方根。
使用这个 ..Heron 的公式
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 操作时尝试使用相同的原始类型。
这是对以下问题的重新提问:
又是任务:
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
作为该区域的答案。我做错了什么?
我已经处理了 7 个多小时,只是因为我不了解其中可能存在的问题。我学习这门 CompSci 课程已经 2 个月了。
你的方法 isValid
returns 如果三角形有效但你假设相反:
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
if (isV)
应该是 if (!isV)
。如果条件不正确,那么您 仅 执行带有潜在危险输入的 Heron 公式,可能导致 s * (s - side1) * (s - side2) * (s - side3)
为负数。 Java 基本类型未定义负数的平方根。
使用这个 ..Heron 的公式
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 操作时尝试使用相同的原始类型。