【问题标题】:Method is returning NaN even though the calculation is correct即使计算正确,方法仍返回 NaN
【发布时间】:2012-11-11 17:16:48
【问题描述】:

当我调用应该返回计算出的角度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 角度B为:NaN

角度 C 为:180.0

周长为:0.0 面积为:0.0

【问题讨论】:

  • 究竟哪个方法返回 NaN?
  • 你的意见是什么?
  • x1 = 1, y1 = 44, x2 = 3, y2 = 55, x3 = 6, y3 = 41
  • getter 更新和依赖实例字段的方式让我头疼。
  • 你没有在Triangle中初始化sideA - sideC,所以当你使用它们时它们是0

标签: java class methods


【解决方案1】:

在您使用它们之前,您的某些变量没有被初始化为正确的值。例如,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);

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

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

【讨论】:

  • 哈 - 是的,它出现在我开始输入我的答案和点击“发布”之间。你必须快点,不是吗!? ;-)
  • 所以这似乎是问题所在 - 您需要为用于除以的任何变量提供值,或者默认情况下它们为 0.0,这会返回双精度数或浮点数的 Nan。
  • 但我认为我的计算将值传递到我的变量中?抱歉,我是新手:-/
  • 只是快速玩一下。当您从通话中调用 getAngleA() 时:angleA = userTriangle.getAngleA();打印出以下方法变量,它们都是0。你在方法中设置的变量:longestSide = sideA;短边1 =边B;短边2 =边C;打印出它们的值: System.out.println("longestSide = " +longestSide); System.out.println("shortSide1 = " + shortSide1); System.out.println("shortSide2 = " + shortSide2); longSide = 0.0 shortSide1 = 0.0 shortSide2 = 0.0 所以它们没有被设置。
  • 我明白你的意思,所以我的方法中的计算不会“转发”到我的下一个方法。嗯……我该怎么解决这个问题?
【解决方案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;
 }

【讨论】:

    【解决方案3】:

    要正确封装您的字段,您需要将所有类变量设置为私有访问,并使用 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 行包含在底部以将您的两侧更新为新值。

    【讨论】:

    • 太棒了!感谢演示:-))))))
    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多