好的,我是 Java 的新手,之前没有任何编程经验。我正在学习 Java 教程,一切正常,直到我在教程的“对象”部分遇到问题。
程序的标题是创建对象演示。目标是找到一个矩形的宽度、高度和面积,以及另一个矩形的新位置。您使用“创建对象”这一前提来完成所有这些工作。创建对象部分是问题所在。
原始代码如下:
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);
^
完整的代码和过程也位于here
同样,错误消息以相同的方式指向单词“Point”和“Rectangle”,并声称它“找不到 [the] symbol”。
如有任何帮助,我们将不胜感激。几天来我一直在为这个错误而苦苦挣扎。谢谢。
最佳答案
您必须将类Point
和Rectangle
导入到您的类中。在类的顶部,package
行之后添加以下两行。
import java.awt.Point;
import java.awt.Rectangle;
如果您使用的是 Eclipse,则只需执行 CtrlShiftO,这将为您导入所需的类。
关于java - 错误 : Cannot Find Symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914703/