gpt4 book ai didi

Java运行程序

转载 作者:行者123 更新时间:2023-12-01 17:01:55 25 4
gpt4 key购买 nike

这是一个非常简单的程序。我创建了一个新类,我将定义一个新方法来在新类中调用下一步。

public class MyClass {

public static void main(String[] args) {
int number = 1;
public void showSomething(){
System.out.println("This is my method "+number+" created by me.");
}

}
}

但是,当我运行这个程序时,我遇到了一个错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token(s), misplaced construct(s)
Syntax error on token "void", @ expected

最佳答案

错误是因为您在另一个方法中声明了一个方法,这里是 main()

更改此:

public static void main(String[] args) {
int number = 1;
public void showSomething(){
System.out.println("This is my method "+number+" created by me.");
}

}

public static void main(String[] args) {
int number = 1;
showSomething(); // call the method showSomething()
}
public static void showSomething(){
System.out.println("This is my method "+number+" created by me.");
}

此外,showSomething() 也应声明为 static,因为 main()static。只能从另一个静态方法调用静态方法

关于Java运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27130752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com