即使计算正确,方法也返回NaN

时间:2012-11-11 17:16:49

标签: java class methods

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

AngleA是:NaN AngleB是:NaN

AngleC为:180.0

周长为:0.0 面积为:0.0

3 个答案:

答案 0 :(得分: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以外的值。

答案 1 :(得分: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;
 }

答案 2 :(得分:2)

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

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

e.g。

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)));
}

话虽如此 - 我猜你在Triangle的情况下你不想让人们在不改变你的初始输入值(你的x,y,z变量)的情况下更新你的测量值,在这种情况下我会完全省略setter并将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()方法,该方法再次为新值运行输入菜单,并再次包含这三行底部将您的边更新为新值。