繁体   English   中英

如何获得doubleValue以获取输出?

[英]How do you get a doubleValue to get an output?

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

我想要的是 -

输入该行的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));

 }
}

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

Double A = f.nextDouble();

这不会将值分配给成员

public static double A;

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

改成

A = f.nextDouble();

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

对于变量A,B,C,您已经声明了static,并且在main方法中再次声明了变量,再次将变量声明为double,这就是为什么A和B的值变为零并出现NaN的原因。

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM