你如何获得doubleValue来获得输出?

时间:2015-03-13 17:09:11

标签: java

我正在上课这个项目,但似乎无法得到答案。

我想要什么 -

输入该行的A值:2.45

输入行的B值:4

输入该行的C值:-8

输入点的x坐标:2.17

输入点的y坐标:-4

从该点到该线的距离为:3.9831092774319026

我得到了什么 - 从点到线的距离是:NaN

import java.io.*;
import java.util.*;
public class DistToline {
public static double A;
public static double B;
public static double C;
public static double distance;

public static double
getDist(double a, double b){
    distance= Math.abs(((A*a)+(B*b)+(C))/(Math.pow(A, 2))+(Math.pow(B, 2)));
    return distance;
 }


 public static void main(String args[])
 {
    Scanner f= new Scanner(System.in);
    System.out.print("Enter the A value for the line:");
    Double A = f.nextDouble();

    Scanner g= new Scanner(System.in);
    System.out.print("Enter the B value for the line:");
    Double B = g.nextDouble();

    Scanner h= new Scanner(System.in);
    System.out.print("Enter the C value for the line:");
    Double C = h.nextDouble();

    Scanner i= new Scanner(System.in);
    System.out.print("Enter the x coordinate of the point:");
    Double X = i.nextDouble();

    Scanner j= new Scanner(System.in);
    System.out.print("Enter the y coordinate of the point:");
    Double Y = j.nextDouble();

    System.out.print("Distance from the point to the line is: ");
    System.out.println(getDist(5,4));

 }
}

从我认为我做错了是因为我没有进行计算并且没有返回距离,因为双重是这就是为什么我没有得到输出?如果是这样,我该如何解决?

2 个答案:

答案 0 :(得分:4)

Double A = f.nextDouble();

这不会将值分配给成员

public static double A;

它创建一个名为A的局部变量。

更改为

A = f.nextDouble();

并重复所有其他成员变量。

答案 1 :(得分:0)

对于变量A,B,C,你已经在main方法中声明了静态,你再次将变量声明为double,这就是为什么A&的值。 B变为零并且NaN出现.Below是您运行的代码,只有很小的变化 -

public class DistToline {
public static double A;
public static double B;
public static double C;
public static double distance;

public static double
getDist(double a, double b){
    distance= Math.abs(((A*a)+(B*b)+(C))/(Math.pow(A, 2))+(Math.pow(B, 2)));
    return distance;
 }



public static void main(String args[])
{
   Scanner f= new Scanner(System.in);
   System.out.print("Enter the A value for the line:");
    A = f.nextDouble();

   Scanner g= new Scanner(System.in);
   System.out.print("Enter the B value for the line:");
    B = g.nextDouble();

   Scanner h= new Scanner(System.in);
   System.out.print("Enter the C value for the line:");
    C = h.nextDouble();

   Scanner i= new Scanner(System.in);
   System.out.print("Enter the x coordinate of the point:");
   Double X = i.nextDouble();

   Scanner j= new Scanner(System.in);
   System.out.print("Enter the y coordinate of the point:");
   Double Y = j.nextDouble();
   System.out.print("Distance from the point to the line is: ");
   System.out.println(getDist(5,4));

}