[英]Error in “Hello World” when compiling on Dr. Java?
I am learning Java, and am testing a simple 'Hello World' program given to me by my teacher. 我正在学习Java,并且正在测试老师给我的一个简单的“ Hello World”程序。 I am using Dr. Java on 64-bit Ubuntu 12.04 LTS.
我在64位Ubuntu 12.04 LTS上使用Java博士。
Code is below: 代码如下:
public class Hello_World
{
public void go()
{
System.out.println("Hello, World!");
}
}
I hit the F5 key, and the code compiles. 我按F5键,然后代码会编译。 After that, I enter the lines below:
之后,我输入以下几行:
greet = new Hello_World();
greet.go();
The output is supposed to be Hello, World!
输出应该是
Hello, World!
, but I am getting Static Error: Undefined name 'greet'
instead. ,但我收到“
Static Error: Undefined name 'greet'
。 What am I doing wrong? 我究竟做错了什么?
Please forgive me if I this is an easy fix (it probably is). 如果这很容易解决,请原谅(可能是)。 I searched SE, but did not find anything that helped.
我搜索了SE,但没有找到任何帮助。
it should be: 它应该是:
Hello_World greet = new Hello_World();
greet.go();
The class you defined is called Hello_Word not Hello. 您定义的类称为Hello_Word而不是Hello。
EDIT 编辑
Your complete code should look something like: 您的完整代码应类似于:
public class Hello_World
{
public void go()
{
System.out.println("Hello, World!");
}
public static void main(String[] args){
Hello_World greet = new Hello_World();
greet.go();
}
}
You need the code: 您需要以下代码:
Hello_World greet = new Hello_World();
greet.go();
in a main method, which is the point of execution of a java program. 在main方法中,这是Java程序的执行点。 http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html
http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html
Add the following code to your class 将以下代码添加到您的课程中
public static void main(String args[]){
Hello_World greet=new Hello_World();
greet.go();
}
Since you are running application on your console, you need to have a main()
method 由于您是在控制台上运行应用程序,因此需要一个
main()
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.