gpt4 book ai didi

java - 即使计算正确,方法仍返回 NaN

转载 作者:行者123 更新时间:2023-12-03 18:17:37 25 4
gpt4 key购买 nike

当我调用应返回计算出的角度 angleA、angleB、angleC 的方法(在另一个类中)时,我得到了结果 NaN。我已经三次检查了我所有的计算,所以我设置程序的方式一定有问题。我做错了什么??

/* Write a Java program enabled to compute and show the following properties of a given triangle :
The individual length of all sides
The angles at all corners
The perimeter
The area

*/

public class Triangle
{
private double x1, x2, x3, y1, y2, y3;
double sideA, sideB, sideC;
private double angleA, angleB, angleC;
double longestSide, shortSide1, shortSide2;
private double perimeter, halfPerimeter, triangleArea;
private String stringLongestSide;

public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}

public double getSideA()
{
return (Math.sqrt(Math.pow((x3-x2),2)+Math.pow((y3-y2),2)));
}

public double getSideB()
{
return (Math.sqrt(Math.pow((x3-x1),2)+Math.pow((y3-y1),2)));
}

public double getSideC()
{
return (Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2)));
}



//Begin by using the cosine rule to find the largest angle
public double getAngleA()
{
//which side is the longest?

longestSide = sideA;
shortSide1 = sideB;
shortSide2 = sideC;

if (longestSide < sideC)
{
longestSide = sideC;
shortSide1 = sideA;
shortSide2 = sideB;
}
else
if (longestSide < sideB)
{
longestSide = sideB;
shortSide1 = sideA;
shortSide2 = sideC;
}

return (Math.acos((Math.pow(shortSide1,2)+Math.pow(shortSide2,2)-Math.pow(longestSide,2))/(2*shortSide1*shortSide2)))*180/Math.PI;
}

//Use the sine rule to find one of the remaining angles
public double getAngleB()
{
return ((Math.asin((shortSide1*Math.sin((angleA*Math.PI/180))/longestSide)))*180/Math.PI);
}

//Use the 'sum of internal angles' rule to find the third angle
public double getAngleC()
{
return (180 - (angleA + angleB));
}


//Calculating the perimeter
public double getPerimeter()
{
return (sideA + sideB + sideC);
}

//Calculating the area of the triangle
public double getArea()
{
halfPerimeter = perimeter/2;
return (Math.sqrt(halfPerimeter*(halfPerimeter-sideA) * (halfPerimeter-sideB) * (halfPerimeter-sideC)));
}

}

调用方法的类:

/* ShellApplication
Rakel Bára Þorvaldsdóttir
*/
import java.util.Scanner;
import java.text.DecimalFormat;

public class Interaction
{
public static void main(String [] args) //required
{
//write your code here
double x1, x2, x3, y1, y2, y3;
double sideA, sideB, sideC;
double angleA, angleB, angleC;
double perimeter, area;

DecimalFormat coordinates = new DecimalFormat ("#");
DecimalFormat calculations = new DecimalFormat ("#.##");

System.out.println("Welcome!");
System.out.println("Please enter the coordinates of your triangle, the x-coordinate first each time and then the y-coordinate.\n\t------------------------\n");

String garbage;

Scanner scan = new Scanner( System.in );

System.out.print( "Enter the x-coordinate for the first point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x1 = scan.nextDouble( );

System.out.print( "Enter the y-coordinate for the first point, and then press Enter: ");
while (! scan.hasNextInt()) //creating a while loop to ensure only integer numbers are accepted
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y1 = scan.nextDouble( );

System.out.print( "Thanks for entering the first point. Now on to the next! \n\t------------------------\n");

System.out.print( "Enter the x-coordinate for the second point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x2 = scan.nextDouble( );

System.out.print( "Enter the y-coordinate for the second point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y2 = scan.nextDouble( );

System.out.print( "Thanks for entering the second point. Just one more left! \n\t------------------------\n");

System.out.print( "Enter the x-coordinate for the third point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
x3 = scan.nextDouble( );

System.out.print( "Enter the y-coordinate for the third point, and then press Enter: ");
while (! scan.hasNextInt())
{
garbage = scan.nextLine();
System.out.println("Please enter an integer.");
}
y3 = scan.nextDouble( );

System.out.println( "You did it! Your triangle has the following coordinates: ");
System.out.println( "Point A: ("+coordinates.format(x1) +" , " +coordinates.format(y1) +")" + " , Point B: ("+coordinates.format(x2) +" , " +coordinates.format(y2) +")" + " , Point C: ("+coordinates.format(x3) +" , " +coordinates.format(y3) +")");

Triangle userTriangle = new Triangle(x1, y1, x2, y2, x3, y3);

sideA = userTriangle.getSideA();
sideB = userTriangle.getSideB();
sideC = userTriangle.getSideC();

angleA = userTriangle.getAngleA();
angleB = userTriangle.getAngleB();
angleC = userTriangle.getAngleC();

perimeter = userTriangle.getPerimeter();
area = userTriangle.getArea();

System.out.println( "-----------------");
System.out.println( "SideA is: " +calculations.format(sideA));
System.out.println( "SideB is: " +calculations.format(sideB));
System.out.println( "SideC is: " +calculations.format(sideC));
System.out.println( "-----------------");

System.out.println( "AngleA is: " +angleA);
System.out.println( "AngleB is: " +angleB);
System.out.println( "AngleC is: " +angleC);
System.out.println( "-----------------");

System.out.println( "Perimeter is: " +perimeter);
System.out.println( "Area is: " +area);


}
}

结果是:


A 面是:14,32SideB 是:5,83

SideC 是:11,18

AngleA 是:NaN角度B为:NaN

AngleC 为:180.0

周长为:0.0面积为:0.0

最佳答案

您的一些变量在您使用它们之前没有被初始化为正确的值。例如,sideA、sideB 和 sideC 在您的 getAngleA() 方法中都是 0.0。

如果您使用它们的默认值(对于类变量: double 显然是 0.0)然后尝试除以它,结果将是 NaN(实际上是无穷大,但是如果您尝试使用无穷大进行计算,您会得到 NaN 作为结果)。

当您从调用中调用 getAngleA() 时:

angleA = userTriangle.getAngleA();

打印出下面的方法变量,都是0.0

您在方法 getAngleA() 中设置的变量:

longestSide = sideA;
shortSide1 = sideB;
shortSide2 = sideC;

设置后立即打印它们的值:

System.out.println("longestSide = " + longestSide);
System.out.println("shortSide1 = " + shortSide1);
System.out.println("shortSide2 = " + shortSide2);

这是打印出来的: 最长边 = 0.0 短边 1 = 0.0 shortSide2 = 0.0

因此它们不会被设置为 0.0 以外的值。

关于java - 即使计算正确,方法仍返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com