简体   繁体   English

为什么我收到错误:在 java 中找不到符号?

[英]Why am I getting error: cannot find symbol in java?

Error Messages [1]: https://i.stack.imgur.com/6Jfk1.jpg [2]: https://i.stack.imgur.com/MfX4T.jpg [3]: https://i.stack.imgur.com/4R6n3.jpg错误消息 [1]: https://i.stack.imgur.com/6Jfk1.jpg [2]: https://i.stack.imgur.com/MfX4T.jpg [3]: https://i.stack.imgur.com/MfX4T.jpg [3]: Z5E056C500A1C4BADE5Z://i.stack.imgur.com/MfX4T.jpg [3]: Z5E056C500AD8044 stack.imgur.com/4R6n3.jpg

I ran these two programs on my laptop and it ran fine.我在笔记本电脑上运行了这两个程序,运行良好。 I decided to download it and send it to my computer but now I am getting an error message.我决定下载它并将其发送到我的计算机,但现在我收到一条错误消息。 I saved both programs in the same file and they are the same version of JGrasp.我将这两个程序保存在同一个文件中,它们是相同版本的 JGrasp。 Any ideas on why this is happening?关于为什么会发生这种情况的任何想法? Should I be importing the class before using it?我应该在使用之前导入 class 吗?

To use the type Die you need to define the type Die in a file called Die.java which contains要使用类型Die ,您需要在名为Die.java的文件中定义类型Die ,其中包含

public class Die {

    ... and some stuff here ...

}

In java you cannot use a type until it is created with some sort of "class " statement, and if that type is in a different package, then you need to import it with the line在 java 中,在使用某种“类”语句创建类型之前,您不能使用类型,如果该类型位于不同的 package 中,则需要使用该行导入它

import some.other.package.Die;

Your class is called PairOfDice , while you're trying to instanciate an object of the Die class.您的 class 称为PairOfDice ,而您正在尝试实例化Die class 的 object 。 Die is only a method of the class mentioned before. Die只是前面提到的class的一种方法。 You'll need to instanciate your die like this:你需要像这样实例化你的 die:

PairOfDice die1 = new PairOfDice();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在Java中出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error in Java? 编译时,为什么在Java程序中出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error in my java program when I compile? 为什么会出现“找不到符号”错误? - Why am I getting 'cannot find symbol' error? 为什么在此代码中出现“找不到符号错误”? - Why I am getting “Cannot find symbol error ” in this code? 为什么会出现错误:找不到JComponent的符号? - Why am i getting error: cannot find symbol for JComponent? 不知道为什么我会收到这个“找不到符号”的错误 - Not sure why I am getting this "cannot find symbol" error (初学者)为什么我得到这个找不到符号错误? - (Beginner) Why am I getting this cannot find symbol error? 在另一个.java文件中定义语句后,为什么会出现“找不到符号”错误? - Why am I getting “Cannot find symbol” error when I already defined the statements in another.java file? 当我想使用 java.io.File 类时,为什么会出现错误“找不到符号”? - Why I am getting error "cannot find symbol" when I would like to use java.io.File class? 为什么我在编译时会得到“找不到符号”? - Why am I getting a “cannot find symbol” when I compile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM