java初学者“Hello World”
我正在努力学习Java。
我不明白为什么这段代码不起作用。
它不会从 test()
函数输出“Hello World
”。
我做错了什么?
public class Main {
public test(args) {
System.out.println(args);
}
public static void main(String[] args) {
test('Hello World');
}
}
I am trying to learn Java.
I don't understand why this code won't work.
It won't output "Hello World
" from test()
function.
What am I doing wrong?
public class Main {
public test(args) {
System.out.println(args);
}
public static void main(String[] args) {
test('Hello World');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
首先:
您需要一个类型来配合参数 - Java 是一种强类型语言,因此您总是需要指定一个类型。至于这里的类型是什么,
System.out.println()
实际上可以接受任何东西,所以你可以将类型设置为String、Object或任何你喜欢的类型(因为Object有一个toString ()
方法,它有很多重载来处理所有原语。)但请记住,这很不寻常,您遇到的大多数方法只会采用特定类型的东西!由于您仅从此处的 main 方法调用 test,并且向其传递一个字符串,因此您也可以将 args 的类型设置为 String。
第二个问题是没有指定返回类型。您始终需要指定返回类型,在这种情况下不会返回任何内容,因此类型为
void
。如果您不这样做,那么编译器将无法知道您编写的内容是方法还是构造函数。第三个问题是 test 是一个实例方法,但您静态地调用它。 test() 也需要是静态的,否则它属于 Main 的实例而不是 Main 类。为什么这很重要?那么,可能有数千个 Main 实例,那么该方法应该在哪个实例上运行呢?编译器无法知道。
下一步:
您要在此处传递一个字符串,该字符串需要用双引号引起来。 Java 对待引号的方式与 PHP 不同,单引号用于单字符文字,双引号用于字符串。所以你永远不能像这样用单引号将字符串括起来,它必须是双引号。
把它们放在一起:
Firstly:
You need a type to go with a parameter - Java is a strongly typed language and thus you always need to specify a type. As to what the type is here,
System.out.println()
can actually take anything, so you could set the type to String, Object or whatever you like (since Object has atoString()
method and it has lots of overloads to deal with all the primitives.) Bear in mind this is unusual though, most of the methods you come across will just take something of a specific type!Since you're only calling test from the main method here, and you're passing a string to it, you may as well set the type of args to String.
The second problem with this is that there's no return type specified. You always need to specify a return type, in this case nothing is returned so the type is
void
. If you don't do this then the compiler has no way of knowing whether what you wrote was meant to be a method or a constructor.The third problem is that test is an instance method but you're calling it statically. test() needs to be static as well, otherwise it belongs to instances of Main and not the Main class. Why does this matter? Well, there could potentially be thousands of instances of Main, so what instance should the method run on? The compiler has no way of knowing.
Next:
You're passing a string here, which needs to be in double quotes. Java treats quotes differently to PHP, single quotes are used for single character literals and double quotes are used for strings. So you can never enclose a string in single quotes like this, it has to be double.
Putting it all together:
在 Java 中,字符串总是使用双引号,而不是单引号。这是为
char
类型保留的。另外,您的测试函数没有返回类型(甚至没有
void
),并且它是一个方法,而 main 是静态的,因此您需要实例化Main
或也使test
静态。您还需要指定每个参数的类型。试试这个:
In Java, Strings are always using double-quotes, never single-quotes. That's reserved for
char
types.Also, your test function doesn't have a return type (not even
void
), and it's a method while main is static, so you'd need to instantiateMain
or maketest
static as well. You also need to specify the type for each argument.Try this:
test() 方法有语法错误
test() 方法缺少返回类型和参数类型。将其更改为例如
“Hello World”的无效语法字符串
需要用双引号括起来,因此将“Hello World”更改为“Hello World”
您不能从类方法调用实例方法。
您的 test() 方法是 Main 类的实例成员,而 main() 是一个类方法。您需要一个 Main 类的实例来调用 test()。例如
,或者,将 test() 更改为类方法
您也可以将 test() 更改为类方法,这样您就可以从 main() 调用它
The test() method has syntax errors
test() method lacks a return type, and the type of the argument. Change it to e.g.
Invalid syntax for 'Hello World'
Strings are need to be enclosed in double quotes, so change 'Hello World' to "Hello World"
You can't call an instance method from a class method.
Your test() method is an instance member of the Main class, while main() is a class method. You'll need an instance of the Main class to call test() on. e.g.
Alternativly, change test() to a class method
You could also make test() a class method, that way you can call it from main()
为类创建一个
对象
并在主方法中访问或将方法声明为static
create an
object
for the class and access in the main method or make the method declaration asstatic
比较:
compare to:
测试应该是静态方法。
从静态方法中,您只能调用静态方法或
创建 Main 的实例并像
Main m = new Main() 一样调用
m.test("你好世界");
此外 hello world 应该用双引号。
test should be a static method.
from a static method u can invoke only static methods or
create an instance of Main and invoke as like
Main m = new Main()
m.test("hello world");
further hello world should be double quotes.
使用 java 开始的简单步骤
使用记事本或任何其他编辑器在 testjava 中创建一个名为 Hello.java 的小文件,文本为
c:\Program Files (x86)\Java\jdk1.6.0_14\bin>
javac c:\testjava\Hello.java
应该在 testjava 中创建一个名为 Hello.class 的类文件
c:\testjava>
java Hello
你好世界
Simple step to start with java
Using Notepad or any other editor create a small file in testjava with name Hello.java with the text as
c:\Program Files (x86)\Java\jdk1.6.0_14\bin>
javac c:\testjava\Hello.java
a class file with name Hello.class should be created in testjava
c:\testjava>
java Hello
Hello, world
您可以将其全部放在主方法中,而不是创建和使用测试方法,这样它看起来像这样:
请注意,双引号用于字符串,因为单引号通常用于字符:)
Instead of creating and using a test method, you can put it all in the main method so it would look like this:
Note that the double quotes are used for strings, as single quotes are normally used for chars :)
现在你的代码应该可以工作了。
Now your code should work.
您可以将该代码写入一个方法中。
例如:
我在课堂上就是这样学的!希望这有帮助!
You can write that code into one method.
For example:
This is how I learned it in my class! Hope this helps!
您必须为测试方法指定返回类型。
您还必须对字符串使用双引号 (
"
)。单引号 ('
) 可用于字符。You have to specify a return type for your test method.
You also must use double quotes (
"
) for Strings. Single quotes ('
) can be used for chars.我修复了您的代码:
好的,让我们看看为什么您的代码不起作用:
您的
test()
方法将args
作为参数。很好,但是您没有说明这个 args 变量的类型是什么。整数
?字符串
?字符
?您需要定义它。对于字符串,您应该使用双引号:
对于字符,您可以使用单引号:
Java 是一种面向对象的语言,这意味着存在具有状态(变量)的对象,以及他们可以做的事情(方法)。当调用
test()
方法时,您并没有将其调用为一个对象。调用
test()
方法时会返回什么?即使它什么也不返回,你也必须编写一个返回类型,void
。最后一点提示,当您遇到麻烦时,只需使用调试器,然后检查程序中的每一步,看看问题出在哪里。
I fixed your code:
Ok, so let's see why your code didn't work:
Your
test()
method hadargs
as a parameter. Great, but you didn't say what is the type of thisargs
variable.Int
?String
?Char
? You need to define that.For strings, you should use the double quotes:
For a char, you would use the single quotes:
Java is an object oriented language, meaning that there are objects with states(variables), and things they can do(methods). When calling the
test()
method, you didn't call it off an object.What does the
test()
method return when you call it? Even if it returns nothing, you have to write a return type,void
.One last tip, when ever you fall in trouble, just use a debugger, and go through every step in your program to see what the problem is.