有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Eclipse方法未定义,即使我定义了它,清理了我的项目,检查了构建路径,并在我的计算机上打孔

我正在为学校编写一些代码(一个测试类和一个初级类),当我创建一个测试类时,我从初级类调用的方法是“未定义CylinderTest类型”。无论如何,当我尝试运行程序时,Eclipse甚至找不到主类

我已经看了这么多的堆栈溢出问题的答案。我也遇到过类似的问题here,但这并不能解决我当前的问题。我还尝试(自己)导入java。util。扫描器进入问题类,但这也不起作用

文件#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);

    }

}

该程序应根据用户输入的圆柱体半径和高度计算圆柱体的体积。事实并非如此


共 (2) 个答案

  1. # 1 楼答案

    以下是修复方法:

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

    二等舱:

    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;
        }
    
    }
    
  2. # 2 楼答案

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

    这其实很简单。只需导入包含这些方法的类,并在调用方法时引用它。要引用一个类,可以声明它的路径和名称,或者,如果导入了它,只需声明它的名称即可

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

    我建议你做一些关于Classes and Objects的补充阅读,看看这个问题: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".

    现在,如果你看一下你的代码,你会发现你的主方法有static修饰符,但没有public修饰符。你最好公开,否则你的程序永远不会运行;)


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

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

    第二,每当您想从System.in读取数据时,都要创建一个新的scanner实例。这最终不是一种威胁,但它效率低下,代码非常“脏”

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

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

    你不应该关闭这个扫描仪