Eclipse 方法未定义,即使我定义了它,清理了我的项目,检查了构建路径,并打了我的电脑

Eclipse method undefined even though I defined it, cleaned my project, checked the buildpath, and punched my computer

我正在为学校(一个测试 class 和一个小学 class)编写一些代码,当我创建一个测试 class 时,我从小学调用的方法class 是 "undefined for type CylinderTest"。无论如何,当我尝试 运行 程序时,Eclipse 甚至找不到主要的 class.

我查看了很多堆栈溢出问题的答案。我遇到过类似的问题 ,但这并不能解决我当前的问题。我也尝试过(自己)将 java.util.Scanner 导入到问题 class 中,但这也没有用

文件 #1

package CirclePackage;
import java.util.Scanner;

public class Cylinder {

    public static double askForRadius() {
        System.out.println("What would you like the radius of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double r = in.nextDouble();
        in.close();
    return r;
    }

public static double askForheight() {
    System.out.println("What would you like the height of the cylinder to be?");
    Scanner in = new Scanner(System.in);
    double h = in.nextDouble();
    in.close();
    return h;
    }

public static double getVolume(double radius, double height) {
    double area = Math.PI*radius*radius*height;
    return area;
    }

}

文件#2:

package CirclePackage;


public class CylinderTest {

static void main(String[] args) {

    double r = askForRadius(); //<--------------Errors appear HERE,
    double h = askForheight(); //<----------------------------HERE,
    double result = getVolume(r, h); //<------------------and HERE.
    System.out.println("The Volume of the cylinder is: " + result);

    }

}

该程序应该根据用户输入的圆柱体半径和高度来计算圆柱体的体积。它没有。

修复如下:

public class CylinderTest {

    public static void main(String[] args) {

        double r = Cylinder.askForRadius(); //<--------------Errors appear HERE,
        double h = Cylinder.askForheight(); //<----------------------------HERE,
        double result = Cylinder.getVolume(r, h); //<------------------and HERE.
        System.out.println("The Volume of the cylinder is: " + result);

    }

}

第二个class:

import java.util.Scanner;

public class Cylinder {

    public static double askForRadius() {
        System.out.println("What would you like the radius of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double r = in.nextDouble();
        return r;
    }

    public static double askForheight() {
        System.out.println("What would you like the height of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double h = in.nextDouble();
        return h;
    }

    public static double getVolume(double radius, double height) {
        double area = Math.PI*radius*radius*height;
        return area;
    }

}

您的主要问题是您试图调用方法 askForRadiusaskForheightgetVolume,而没有在未定义的 class 中使用引用.因此,编译器将在 class CylinderTest 中搜索这些方法,如果找不到,就会产生错误。在编译器可以编译您的代码之前,您需要指定这些方法的位置。

要做到这一点其实很简单。所有需要做的就是导入包含这些方法的 class 并在调用方法时引用它。要引用 class,请说明其路径和名称,或者,如果它是导入的,则只需说明其名称。

double r = Cylinder.askForRadius();
double h = Cylinder.askForheight();
double result = Cylinder.getVolume(r, h);

我建议你在Classes and Objects, and take a look at this question: What are classes, references and objects?

上做一些补充阅读

这里的另一个 main 问题是您的 main 方法。如果您查看 Oracle Tutorial,您会看到它说:

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

现在,如果您查看您的代码,您会发现您的 main 方法具有 static 修饰符,但没有 public 修饰符。你最好让它 public 否则你的程序永远不会 运行 ;)


好了,这解决了两个主要问题,但是你还有两个小问题。

首先,您将在 askForRadius 方法中关闭 System.in,因此当您调用 askForheight 方法时将无法读取它。

其次,每次要从 System.in 中读取时,您都在创建一个新的扫描器实例。这最终不是威胁,但效率低下且非常 "dirty" 代码。

下面两个问题的解决方案已经在 an answer on another question 中定义:

You should create just one Scanner which you use for the life of the program

而且你不应该关闭这个扫描仪。