【问题标题】:"class X is public, should be in X.java" with Ideone online compiler带有 Ideone 在线编译器的“类 X 是公共的,应该在 X.java 中”
【发布时间】:2016-03-07 15:13:04
【问题描述】:

我是一个对编程非常陌生的学生,并且已经用java编写了一个简单的程序来转换单位。

我在 Mac 上使用 ideone 在线编译器。我收到此错误

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

这是我的代码(这是我正在处理的任务,因此得名):

package assessmentOne.pkg1;
import java.util.Scanner;

// MY NAME Assessment One


public class AssessmentOne{

    public static void main(String[] args)
    { int choice;

    do

    { Scanner input = new Scanner(System.in);
    System.out.println("1 = centimetre to inch, 2 = metres to feet, 3 = finish");
    choice = Integer.parseInt(input.next());

    if (choice == 1)

        {
        // converts centimetre to inch
        System.out.println("centimetre to inch");
            double centimetre;
            double inch;

            inch = 0;
            centimetre = Double.parseDouble(input.next());
            System.out.println("inch = centimetre * 0.3973");
        }

    else if (choice == 2)

        {
        // converts metre to feet
        System.out.println("metre to feet");
        double metre;
        double feet;

        feet = 0;
        metre = Double.parseDouble(input.next());
        System.out.println("feet = metre * 3.28");
        }

    else if (choice == 3)

        {
        // exits
        break;
        }

    } while (choice != 3);

    }
    }

【问题讨论】:

  • 将文件重命名为AssessmentOne.java
  • 欢迎来到 Stack Overflow!我编辑了您问题的标题以包含确切的错误消息,因此更多了解该主题的人会看到它。我还将您的代码示例缩进了 4 个空格,以便正确呈现 - 请参阅编辑帮助以获取有关格式的更多信息。最后,我将您使用 Ideone 在线编译器的事实用粗体表示,因为我认为这在这种情况下很重要。祝你好运!
  • 看起来 ideone 坚持你的公共类必须被称为 Main。请在此处查看此答案-您可能想考虑使用适当的完整开发环境,例如 Eclipse 或 IntelliJ stackoverflow.com/questions/22340309/

标签: java file class public


【解决方案1】:

正如该网站上的一个新 Java 项目所说:

/* Name of the class has to be "Main" only if the class is public. */

要么:

  1. 删除public 部分:

    class AssessmentOne{
    
  2. 将类重命名为Main

    public class Main{
    

【讨论】:

    【解决方案2】:

    在下面的代码行中删除 public 并且它可以立即工作。

    “公开课AssessmentOne{”

    【讨论】:

      【解决方案3】:

      只需从“public class AssessmentOne{”中删除“public”即可。换句话说,只需写“class AssessmentOne {”,瞧!它会起作用的。 我遇到了同样的问题,删除“公众”有效。这是具有相同问题的代码的链接。 Click to see

      package assessmentOne.pkg1;
      import java.util.Scanner;
      
      // MY NAME Assessment One
      
      
      class AssessmentOne{
      
        public static void main(String[] args)
        { int choice;
      
          do
      
          { Scanner input = new Scanner(System.in);
            System.out.println("1 = centimetre to inch, 2 = metres to feet, 3 = finish");
            choice = Integer.parseInt(input.next());
      
            if (choice == 1)
      
            {
              // converts centimetre to inch
              System.out.println("centimetre to inch");
              double centimetre;
              double inch;
      
              inch = 0;
              centimetre = Double.parseDouble(input.next());
              System.out.println("inch = centimetre * 0.3973");
            }
      
            else if (choice == 2)
      
            {
              // converts metre to feet
              System.out.println("metre to feet");
              double metre;
              double feet;
      
              feet = 0;
              metre = Double.parseDouble(input.next());
              System.out.println("feet = metre * 3.28");
            }
      
            else if (choice == 3)
      
            {
              // exits
              break;
            }
      
          } while (choice != 3);
      
        }
      }
      

      【讨论】:

        【解决方案4】:

        我不确定,但是当 java 文件的名称与类名不同时,通常会出现此错误。当您将您的类声明为public 时,包含它的java 文件必须与.java 同名。

        示例

        public class Example {}
        

        Example.java

        【讨论】:

        • 我从 public class AssessmentOne { 更改为 public class AssessmentOne.java { 我的错误是 Compilation error time: 0 memory: 0 signal:0 Main.java:5: error: '{' expected public class AssessmentOne.java { ^ 1 错误 感谢您的回复
        • 不,您必须使用public class AssessmentOne {,并且您编写它的文件必须命名为AssessmentOne.java。文件名必须与公共类相同。
        • 您无法更改或查看 Ideone.com 上的文件名。
        猜你喜欢
        • 1970-01-01
        • 2022-10-23
        • 1970-01-01
        • 2014-05-14
        • 2016-04-08
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        相关资源
        最近更新 更多