错误:类X是public应该在名为X.java的文件中声明

问题描述:

我正在尝试编写一个程序,但是我收到了这个编译错误:

I am trying to write a program, but I'm getting this compiler error:

Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java
public class WeatherArray {
       ^
1 error

我检查了我的文件名,我的公共类与我的.java文件相同。

I have checked my file names, and my public class is the same as my .java file.

我该如何解决这个问题?

How can I fix this?

这是我的代码:

public class WeatherArray {
    public static void main(String[] args) {
        // ...
    }
}


您必须将文件重命名为 Main。 java WeatherArray.java 或将类重命名为 public class Main {

You have to rename your file to from Main.java to WeatherArray.java or rename the class to public class Main {