创建Tester类将无法编译

时间:2015-03-18 04:07:28

标签: java java.util.scanner

我的代码有问题,每当我尝试创建Tester类时,代码都无法编译(我的猜测是它们没有相互链接)。然而,当他们不是测试者形式时,他们完美地工作。

这是我的代码:

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:");
        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(2.17, -4));

    }
}

这是我的完整代码,但是当我创建Tester类时,代码不会相互链接。我想这是因为我还没有宣布。

这是我尝试创建测试程序类 -

测试仪:

class Tester{
    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(2.17, -4));

    }
}

班级:

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

代码没有说明任何变量,它们甚至没有相互链接。这是因为他们没有被宣布?如果是这样,我将如何修复它们以便它们相互链接?

1 个答案:

答案 0 :(得分:0)

问题是由于您的getDist()函数位于DistToline类中,但您的主线(void main())位于Tester类中。您需要通过明确指定其所在的类来调用getDist()函数,如下所示:

System.out.println(DistToline.getDist(2.17, -4));

请注意这是有效的,因为getDist()被声明为static。如果getDist()是非静态的,则必须实例化DistToline类并从实例中调用该函数。

在主线中,您还需要以相同的方式引用变量ABC

DistToline.A = f.NextDouble();

这是因为它们是DistToline类的静态成员。

还有一件事,虽然这不会影响结果:您的public static distance变量可以而且应该移到getDist()函数中,因为它不需要是静态的。虽然您可以完全消除它,但直接返回Math.abs的结果:

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