java初学者“Hello World”

发布于 2024-10-18 06:05:20 字数 319 浏览 8 评论 0原文

我正在努力学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

浪荡不羁 2024-10-25 06:05:20

首先:

public test(args) {
    System.out.println(args);
}

您需要一个类型来配合参数 - Java 是一种强类型语言,因此您总是需要指定一个类型。至于这里的类型是什么,System.out.println()实际上可以接受任何东西,所以你可以将类型设置为String、Object或任何你喜欢的类型(因为Object有一个toString () 方法,它有很多重载来处理所有原语。)但请记住,这很不寻常,您遇到的大多数方法只会采用特定类型的东西!

由于您仅从此处的 main 方法调用 test,并且向其传递一个字符串,因此您也可以将 args 的类型设置为 String。

第二个问题是没有指定返回类型。您始终需要指定返回类型,在这种情况下不会返回任何内容,因此类型为 void。如果您不这样做,那么编译器将无法知道您编写的内容是方法还是构造函数。

第三个问题是 test 是一个实例方法,但您静态地调用它。 test() 也需要是静态的,否则它属于 Main 的实例而不是 Main 类。为什么这很重要?那么,可能有数千个 Main 实例,那么该方法应该在哪个实例上运行呢?编译器无法知道。

下一步:

public static void main(String[] args) {
    test('Hello World');
}

您要在此处传递一个字符串,该字符串需要用双引号引起来。 Java 对待引号的方式与 PHP 不同,单引号用于单字符文字,双引号用于字符串。所以你永远不能像这样用单引号将字符串括起来,它必须是双引号。

把它们放在一起:

public class Main {

    public static void test(String args) { //Add return type and parameter type, make test static
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Change single quotes to double quotes
    }
}

Firstly:

public test(args) {
    System.out.println(args);
}

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 a toString() 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:

public static void main(String[] args) {
    test('Hello World');
}

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:

public class Main {

    public static void test(String args) { //Add return type and parameter type, make test static
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Change single quotes to double quotes
    }
}
江湖正好 2024-10-25 06:05:20

在 Java 中,字符串总是使用双引号,而不是单引号。这是为 char 类型保留的。

test("Hello World");

另外,您的测试函数没有返回类型(甚至没有 void),并且它是一个方法,而 main 是静态的,因此您需要实例化 Main 或也使 test 静态。您还需要指定每个参数的类型。

试试这个:

public static void test(String args) {
    System.out.println(args);
}

In Java, Strings are always using double-quotes, never single-quotes. That's reserved for char types.

test("Hello World");

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 instantiate Main or make test static as well. You also need to specify the type for each argument.

Try this:

public static void test(String args) {
    System.out.println(args);
}
尸血腥色 2024-10-25 06:05:20

test() 方法有语法错误

test() 方法缺少返回类型和参数类型。将其更改为例如

public void test(String args) {
    System.out.println(args);
}

“Hello World”的无效语法字符串

需要用双引号括起来,因此将“Hello World”更改为“Hello World”

您不能从类方法调用实例方法。

您的 test() 方法是 Main 类的实例成员,而 main() 是一个类方法。您需要一个 Main 类的实例来调用 test()。例如

public static void main(String[] args) {
        new Main().test("Hello World");
}

,或者,将 test() 更改为类方法

您也可以将 test() 更改为类方法,这样您就可以从 main() 调用它

public static void test(String args) {
    System.out.println(args);
}

The test() method has syntax errors

test() method lacks a return type, and the type of the argument. Change it to e.g.

public void test(String args) {
    System.out.println(args);
}

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.

public static void main(String[] args) {
        new Main().test("Hello World");
}

Alternativly, change test() to a class method

You could also make test() a class method, that way you can call it from main()

public static void test(String args) {
    System.out.println(args);
}
小姐丶请自重 2024-10-25 06:05:20

为类创建一个对象并在主方法中访问或将方法声明为static

create an object for the class and access in the main method or make the method declaration as static

颜漓半夏 2024-10-25 06:05:20

比较:

public class Main {

    public static void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World");
    }
}

compare to:

public class Main {

    public static void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World");
    }
}
醉城メ夜风 2024-10-25 06:05:20
public class HelloWorld {
public void test(String str) {
    System.out.println(str);
}
public static void main(String[] args) {
    HelloWorld helloWorld = new HelloWorld();
    helloWorld.test("Hello World");
}
}
  • Java 中的方法在其名称前有一个返回类型或 void。例如:上面代码中的 void 。
  • 方法参数的名称之前应包含数据类型。例如:上面代码中的字符串。
  • 您必须创建类的实例并使用instance.methodName 调用该方法,或者将该方法声明为静态并使用Classname.methodName 调用它。
  • 作为一个好的做法,类名以大写字母开头,方法名称以小写字母开头。
public class HelloWorld {
public void test(String str) {
    System.out.println(str);
}
public static void main(String[] args) {
    HelloWorld helloWorld = new HelloWorld();
    helloWorld.test("Hello World");
}
}
  • Methods in Java have a return type or void before their name. eg: void in above code.
  • Method arguments should have the data type before their name. eg: String in above code.
  • Either you have to create an instance of your class and call the method using instance.methodName or declare the method as static and call it using Classname.methodName.
  • As a good practice, your class name starts with Uppercase and method name starts with lower case letter.
倥絔 2024-10-25 06:05:20

测试应该是静态方法。
从静态方法中,您只能调用静态方法或
创建 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.

无可置疑 2024-10-25 06:05:20

使用 java 开始的简单步骤

  1. 在 c 中创建一个名为 testjava 的临时文件夹:
  2. 使用记事本或任何其他编辑器在 testjava 中创建一个名为 Hello.java 的小文件,文本为

    类你好{
        公共静态无效主(字符串[] args){
            System.out.println("你好,世界");
        }
    }
    
  3. Go to the path from cmd
    c:\Program Files (x86)\Java\jdk1.6.0_14\bin>
  4. 编译您编写的代码
    javac c:\testjava\Hello.java
    应该在 testjava 中创建一个名为 Hello.class 的类文件
  5. 转到​​路径
    c:\testjava>
  6. 运行命令为
    java Hello
  7. 它应该打印为
    你好世界

Simple step to start with java

  1. Create a temporary folder as testjava in c:
  2. Using Notepad or any other editor create a small file in testjava with name Hello.java with the text as

    class Hello {
        public static void main(String[] args) {
            System.out.println("Hello, world");
        }
    }
    
  3. Go to the path from cmd
    c:\Program Files (x86)\Java\jdk1.6.0_14\bin>
  4. Compile the code you have written
    javac c:\testjava\Hello.java
    a class file with name Hello.class should be created in testjava
  5. Go to the path
    c:\testjava>
  6. Run the command as
    java Hello
  7. It should print as
    Hello, world
绝不放开 2024-10-25 06:05:20

您可以将其全部放在主方法中,而不是创建和使用测试方法,这样它看起来像这样:

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("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:

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}

Note that the double quotes are used for strings, as single quotes are normally used for chars :)

暮凉 2024-10-25 06:05:20
public class Main {

 // You always have to have a return type for a function, at least void
 //Also, the datatype of every variable inside the parantheses () of a function must be defined

    public void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Strings are always passed between " and ", not ' and '
    }
}

现在你的代码应该可以工作了。

public class Main {

 // You always have to have a return type for a function, at least void
 //Also, the datatype of every variable inside the parantheses () of a function must be defined

    public void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        test("Hello World"); //Strings are always passed between " and ", not ' and '
    }
}

Now your code should work.

∞觅青森が 2024-10-25 06:05:20

您可以将该代码写入一个方法中。

例如:

class Test {
     public static void main(String[] args){
        System.out.println("Hello, world!");
     }
}

我在课堂上就是这样学的!希望这有帮助!

You can write that code into one method.

For example:

class Test {
     public static void main(String[] args){
        System.out.println("Hello, world!");
     }
}

This is how I learned it in my class! Hope this helps!

云裳 2024-10-25 06:05:20

您必须为测试方法指定返回类型。
您还必须对字符串使用双引号 (")。单引号 (') 可用于字符。

public void test(String args){
  System.out.println(args);
}

public static void main(String[] args){
  test("Hello World");
}

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.

public void test(String args){
  System.out.println(args);
}

public static void main(String[] args){
  test("Hello World");
}
快乐很简单 2024-10-25 06:05:20

我修复了您的代码:

public class Main {

    private void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        Main TestObject = new Main();
        obj.test("Hello World");
    }
}

好的,让我们看看为什么您的代码不起作用:

  1. 您的 test() 方法将 args 作为参数。很好,但是您没有说明这个 args 变量的类型是什么。 整数字符串字符?您需要定义它。

  2. 对于字符串,您应该使用双引号:

    字符串名称 = "Adam";
    

    对于字符,您可以使用单引号:

    字符键 = 'A';
    
  3. Java 是一种面向对象的语言,这意味着存在具有状态(变量)的对象,以及他们可以做的事情(方法)。当调用 test() 方法时,您并没有将其调用为一个对象。

  4. 调用 test() 方法时会返回什么?即使它什么也不返回,你也必须编写一个返回类型,void

最后一点提示,当您遇到麻烦时,只需使用调试器,然后检查程序中的每一步,看看问题出在哪里。

I fixed your code:

public class Main {

    private void test(String args) {
        System.out.println(args);
    }

    public static void main(String[] args) {
        Main TestObject = new Main();
        obj.test("Hello World");
    }
}

Ok, so let's see why your code didn't work:

  1. Your test() method had args as a parameter. Great, but you didn't say what is the type of this args variable. Int? String? Char? You need to define that.

  2. For strings, you should use the double quotes:

    String name = "Adam";
    

    For a char, you would use the single quotes:

    char key = 'A';
    
  3. 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.

  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文