1

好的,所以我对 Java 很陌生,而且我之前没有编程经验。我正在阅读 Java 教程,一切都很好,直到我在教程的“对象”部分遇到问题。

该程序的标题是 Create Object Demo。目标是找到一个矩形的宽度、高度和面积,以及另一个矩形的新位置。您可以在“创建对象”的前提下完成所有这些工作。创建对象部分是问题所在。

这是原始代码:

public class CreateObjectDemo {

public static void main(String[] args) {

    // Declare and create a point object and two rectangle objects.
    Point originOne = new Point(23, 94);
    Rectangle rectOne = new Rectangle(originOne, 100, 200);
    Rectangle rectTwo = new Rectangle(50, 100);

    // display rectOne's width, height, and area
    System.out.println("Width of rectOne: " + rectOne.width);
    System.out.println("Height of rectOne: " + rectOne.height);
    System.out.println("Area of rectOne: " + rectOne.getArea());

    // set rectTwo's position
    rectTwo.origin = originOne;

    // display rectTwo's position
    System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
    System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);

    // move rectTwo and display its new position
    rectTwo.move(40, 72);
    System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
    System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}

我运行程序,这是我的错误信息:

CreateObjectDemo:.java:6: error: cannot find symbol
       Point originOne = new Point(23, 94);
       ^ 

symbol: class Point
location: class CreateObjectDemo
CreatObjectDemo.java:6: error: cannot find symbol
    Point originOne = new Point(23, 94);
                          ^

完整的代码和流程也在这里

同样,错误消息以相同的方式指向单词“Point”和“Rectangle”,并声称它“找不到 [the] symbol”。

任何帮助将不胜感激。几天来,我一直在为这个错误而苦苦挣扎。谢谢。

4

4 回答 4

3

您将不得不将课程导入到您的课程PointRectangle。将以下两行添加到类顶部的package行之后。

import java.awt.Point;
import java.awt.Rectangle;

如果您使用的是 Eclipse,那么就这样做CtrlShiftO,这将为您导入所需的类。

于 2013-07-29T01:25:07.057 回答
1

正如 Roddy 建议的那样,您需要下载并在您所遵循的教程中包含PointRectangle类。如果您将这些类放在与您相同的目录中,则CreateObjectDemo不需要导入语句。

究竟发生了什么是编译器试图将您的 Java 源代码转换为 JVM(Java 虚拟机)可以解释的字节码。要编译代码,必须找到所有类。由于找不到PointRectangle类,您的代码无法编译。由于这个原因 ,类PointRectangle被称为类的依赖项。没有它们就行不通,它依赖于and 。CreateObjectDemoCreateObjectDemoPointRectangle

解决这个问题很容易,只需确保您的CreateObjectDemo班级知道PointRectangle班级在哪里。

于 2013-07-29T01:30:04.047 回答
0

对于 CreateObject Demo,教程中还有两个其他类需要放在与演示相同的目录中:Point 类和 Rectangle 类。它们是在演示代码的后面几段描述的。

所以在netbeans和eclipse中,把它们和CreateObjectDemo.java放在同一个目录下。

所以在 netbeans 中,转到 File->New File->Java->Java Class->Class Name: Point 等

你不需要导入任何东西。

希望这可以帮助。

于 2013-09-04T05:07:10.537 回答
0

The tutorial is not learning about GUI but just showing how to calculate the rectangle area using a method, so it is not necessary to import java.awt in the CreateObjectDemo.class. Just make sure that the Point and Rectangle classes are put in the same package with the CreateObjectDemo class, the error will surely disappear.

于 2017-07-16T15:40:46.280 回答