2

当我调用应该返回计算出的角度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);


    }
}

结果是:


SideA 是:14,32 SideB 是:5,83

SideC 是:11,18

角度A 是:NaN 角度B 是:NaN

角度C为:180.0

周长:0.0 面积:0.0

4

3 回答 3

3

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

如果您使用它们的默认值(对于类变量显然是 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);

这是打印出来的:longestSide = 0.0 shortSide1 = 0.0 shortSide2 = 0.0

所以它们没有被设置为 0.0 以外的值。

于 2012-11-11T17:26:29.860 回答
2

要正确封装您的字段,您需要将所有类变量设置为私有访问,并使用 getter 和 setter 来设置和获取值。

因此,为了扩展 Sura 的答案,您可以将这些方法添加到您的 Triangle 类中,并将您的 sideA、sideB 和 sideC(以及所有其他类变量)设置为私有

例如

public double getSideA()
{
  return sideA;
}

public double getSideB()
{
  return sideB;
}

public double getSideC()
{
  return sideC;
}

public void setSideA()
{
  sideA = (Math.sqrt(Math.pow((x3-x2),2)+Math.pow((y3-y2),2)));
}

public void setSideB()
{
  sideB =  (Math.sqrt(Math.pow((x3-x1),2)+Math.pow((y3-y1),2)));
}

public void setSideC()
{
  sideC = (Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2)));
}

话虽如此 - 我猜在你的三角形情况下你不想让人们在不改变你的初始输入值(你的 x、y、z 变量)的情况下更新你的侧面测量值,在这种情况下我会省略设置器完全并将 Sura 的 3 条侧面设置线添加到构造函数的底部,因此它们是在初始化对象时设置的。

sideA = (Math.sqrt(Math.pow((x3-x2),2)+Math.pow((y3-y2),2))); 
sideB = (Math.sqrt(Math.pow((x3-x1),2)+Math.pow((y3-y1),2)));  
sideC = (Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2)));

如果您希望人们能够更改三角形的尺寸,我建议只使用一个 updateTriangle() 方法,该方法再次运行您的输入菜单以获取新值,并再次在底部包含这 3 行以进行更新支持新价值观。

于 2012-11-11T19:15:14.027 回答
2

这是你在 Trangle 类中必须做的改变。在每个 Side 获取方法而不是返回值中,您必须首先将值分配给 sideA、sideB、sideC 然后返回

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

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

 public double getSideC()
 {
 sideC = (Math.sqrt(Math.pow((x2-x1),2)+ Math.pow((y2-y1),2)));
 return sideC;
 }
于 2012-11-11T17:48:40.020 回答