错误 .java:23:错误:类、接口或枚举预期导入 java.util.*;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32542874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:21:10  来源:igfitidea点击:

Error .java:23: error: class, interface, or enum expected import java.util.*;

java

提问by dukej

I am not sure why I get continuing told the error of java22 in my code. I searched other person's question like around. like here Compiler error: "class, interface, or enum expected"

我不确定为什么我会继续在我的代码中告知 java22 的错误。我在周围搜索了其他人的问题。像这里 编译器错误:“类、接口或枚举预期”

But I could not find suitable answer for this. It seems like error not only class, interface or enum import part. Some cording has issue it. But but if so I would like to know, which part is course of the problem in this case?

但我找不到合适的答案。似乎错误不仅是类、接口或枚举导入部分。一些电线有问题。但是,如果是这样,我想知道,在这种情况下,哪一部分是问题的过程?

Also, I would like to know the if I want disable to use the num2 for case2. Since it is factorial, it should ask only num1.

另外,我想知道是否要禁用将 num2 用于 case2。由于它是阶乘,它应该只询问 num1。

Thank you for all the help.

谢谢大家的帮助。

This is the message

这是消息

Calc.java:22: error: class, interface, or enum expected
import java.util.*;
^
1 error

This is a code part

这是代码部分

import java.util.*;
public class Calc
{
public static void main(String[] args)
{
    NumCalc calc = new NumCalc();
    System.out.println("Welcome to Math NumCalc!\nPlease choose an option:" 
    + "\n\t1 " + "- add two real numbers" 
    + "\n\t2 " + "- subtract two real numbers"
    + "\n\t3 " + "- multiply two real numbers"
    + "\n\t4 " + "- devide two real numbers"
    + "\n\t5 " + "- get the factorial of an number"
    + "\n\t6 " + "- menu"
    + "\n\t0 " + "- exit"
    );
    calc.run();
}
}

import java.util.*;
public class NumCalc 
{
private int option = -1;
private Scanner scan, num1, num2;
//private int opt;
double ans  = 0.0;
//int category;

public NumCalc()
{
    scan = new Scanner(System.in); 
}

// entry point for class
public void run()
{
    System.out.println("\n? " + scan);
    int scan = scan.nextInt();

    for (int i=0;i < count; i++)
    {
        option = option + scan.nextInt(); 
    }

    System.out.println("Enter 1st num:");
    int num1 = scan.nextInt();
    System.out.println("Enter 2nd num");
    int num2 = scan.nextInt();

    switch (option)
    {
        case 6:
        ans = num1 + num2;
        System.out.println(num1 + " + " + num2 + "=" + ans);
        break;
        case 5:
        ans = num1 - num2;
        System.out.println(num1 + " - " + num2 + "=" + ans); 
        break;
        case 4:
        ans = num1 * num2;
        System.out.println(num1 + " * " + num2 + "=" + ans);
        break;            
        case 3:
        ans = num1 / num2;
        System.out.println(num1 + " / " + num2 + "=" + ans);
        break;
        case 2:
        num2 = boolean(false); // No 2nd number.
        int ans = 1; 
        for (i = 1;i <= n; i++)
        {
            ans *= n;
        }
        break;
        System.out.println("Factorial of" + num1 + " = ");
        case 1:
        //opt = opt + scan.nextInt();
        continue;
        default:
        close();
    }
}
}

回答by Kevin J. Chase

The simple solution, as other people have said, is to split your two publicclasses into two separate .javafiles. That will solve both the error you've already encountered and another that you were going to encounter later... but "too many public classes" is notwhat your Java compiler is complaining about. The real reason you're getting that error is that you can't put importstatements after classdeclarations.

正如其他人所说,简单的解决方案是将您的两个public类拆分为两个单独的.java文件。这将解决您已经遇到的错误和您稍后将遇到的另一个错误……但是“公共类太多”并不是您的 Java 编译器所抱怨的。您收到该错误的真正原因是您不能importclassDeclarations之后放置statements

The error message is easy to misread. It seems to be complaining about whatyou're importing --- probably provoking a response like, "But I amimporting a class!". But note where the ^is pointing --- not at java.util.*, but at the word import:

错误信息很容易被误读。这似乎是在抱怨什么,你要导入---可能挑起类似这样的回复,“但是我导入类!”。但请注意^指向的位置 --- 不是在java.util.*,而是在单词import

Calc.java:22: error: class, interface, or enum expected
    import java.util.*;
    ^
1 error

This error is actually complaining about the out-of-place importstatement --- it shouldn't be there at all. Once you've declared your first class, it's too late to importanything else. In a roundabout way, it actually says so: By line 22 of Calc.java, nothing can appear at the top level but type declarations --- the "interface, class, or enum" it mentioned --- so encountering a line that starts with importis "unexpected", meaning not allowed.

这个错误实际上是在抱怨不合时宜的import声明——它根本不应该存在。一旦你宣布了你的第一个classimport其他任何事情都为时已晚。以一种迂回的方式,它实际上是这样说的:在 的第 22 行Calc.java,除了类型声明之外,什么都不能出现在顶层——它提到的“ interface、”classenum“——所以遇到以 开头的行import是“意外” ,表示不允许。

You can verify this by commenting out the second import(line 22) and compiling. You'll still get errors, but different ones.

您可以通过注释掉第二个import(第 22 行)并编译来验证这一点。您仍然会遇到错误,但会有所不同。

As an aside, you areallowed to import java.util.*;more than once. The redundant statements will be ignored... as long as they're in the right place. You can verify this by moving the second importup to around line 3 or 4 and compiling. Again, you'll still get errors, but not about that.

顺便说一句,你不准import java.util.*;超过一次。多余的语句将被忽略……只要它们在正确的位置。您可以通过将第二个import向上移动到第3 行或第 4 行并编译来验证这一点。同样,您仍然会遇到错误,但不是关于那个。

The Correct Order

正确的顺序

A single .javafile (called a "compilation unit" in the language spec) has the following three parts, which mustappear in this order:

单个.java文件(在语言规范中称为“编译单元”)具有以下三个部分,它们必须按此顺序出现:

  1. Zero or one packagedeclarations.
  2. Zero or more importstatements.
  3. Zero or more top-level type declarations, like class, enum, or interface.
  1. 零个或一个package声明。
  2. 零个或多个import语句。
  3. 零个或多个顶级类型声明,如classenuminterface

From the Java 1.8 Language Specification, Section 7.3: "Compilation Units"]:

来自 Java 1.8 语言规范,第 7.3 节:“编译单元”]:

CompilationUnitis the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:

CompilationUnit: [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

A compilation unit consists of three parts, each of which is optional[...]

CompilationUnit是目标符号(§2.1的句法文法()§2.3Java程序)。它由以下产生式定义:

CompilationUnit: [ PackageDeclaration] { ImportDeclaration} { TypeDeclaration}

一个编译单元由三部分组成,每部分都是可选的[...]

In case that last phrase made you wonder: Yes, a completely empty .javafile willcompile without warnings... and without producing any .classfiles.

如果最后一句话让你想知道:是的,一个完全空的.java文件将在没有警告的情况下编译......并且不会产生任何.class文件。

Solutions

解决方案

As has been pointed out by others, you can't have more than one top-level publicclass or other type declaration in a single file. Unless you're willing to nest one of Calcand NumCalcinside the other, they have to split up into Calc.javaand NumCalc.java.

正如其他人指出的那样,public在一个文件中不能有多个顶级类或其他类型声明。除非你愿意窝一个CalcNumCalc内部另一方面,他们不得不分割成Calc.javaNumCalc.java

Actually, you couldsidestep the problem by reducing one class's visibility to package-default, but that's fragile and not the way Java is generally done. If you tried to use the package-default class in any other .javafile, even one in the same package, it would fail to compile because it couldn't find the supposedly-package-visible class --- either Calcwould be in the wrongly-named NumCalc.java(notwhere the compiler will look for it), or NumCalcwould be hiding out inside Calc.java.

实际上,您可以通过减少一个类对默认包的可见性来回避这个问题,但这是脆弱的,而不是 Java 通常的做法。如果您尝试在任何其他.java文件中使用 package-default 类,即使是在同一个包中,它也将无法编译,因为它找不到所谓的 package-visible 类 --- 要么Calc是错误的 -命名NumCalc.java不是编译器会在哪里寻找它),或者NumCalc会隐藏在Calc.java.

But why bother with all that? You could combine the two classes into one classvery easily, and have a more coherent project. (I'm not sure why they're separate classes in the first place.)

但是为什么要费心呢?您可以很容易地将两个类合并为一个类,并拥有一个更加连贯的项目。(我不确定为什么它们首先是单独的类。)