编译器错误:“类,接口,或枚举预期”(Compiler error: “class, interfa

2019-06-18 10:37发布

我一直在解决此方案小时,尝试多种配置,以及有没有运气。 它已被Java编写的,并具有33点的误差(从50之前降低)

源代码:

/*This program is named derivativeQuiz.java, stored on a network drive I have permission to edit
The actual code starts below this line (with the first import statement) */
import java.util.Random;
import java.Math.*;
import javax.swing.JOptionPane;
public static void derivativeQuiz(String args[])
{
    // a bunch of code
}

错误日志(在JCreator的编译):

--------------------Configuration: <Default>--------------------
H:\Derivative quiz\derivativeQuiz.java:4: class, interface, or enum expected
public static void derivativeQuiz(String args[])
              ^
H:\Derivative quiz\derivativeQuiz.java:9: class, interface, or enum expected
    int maxCoef = 15;
    ^
H:\Derivative quiz\derivativeQuiz.java:10: class, interface, or enum expected
    int question = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of questions you wish to test on: "));
    ^
H:\Derivative quiz\derivativeQuiz.java:11: class, interface, or enum expected
    int numExp = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the maximum exponent allowed (up to 5 supported):" ));
    ^
H:\Derivative quiz\derivativeQuiz.java:12: class, interface, or enum expected
    Random random = new Random();
    ^
H:\Derivative quiz\derivativeQuiz.java:13: class, interface, or enum expected
    int coeff;
    ^
H:\Derivative quiz\derivativeQuiz.java:14: class, interface, or enum expected
    String equation = "";
    ^
H:\Derivative quiz\derivativeQuiz.java:15: class, interface, or enum expected
    String deriv = "";
    ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
    ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                   ^
H:\Derivative quiz\derivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                                  ^
H:\Derivative quiz\derivativeQuiz.java:19: class, interface, or enum expected
        deriv = "";
        ^
H:\Derivative quiz\derivativeQuiz.java:20: class, interface, or enum expected
        if(numExp >= 5)
        ^
H:\Derivative quiz\derivativeQuiz.java:23: class, interface, or enum expected
            equation = coeff + "X^5 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:24: class, interface, or enum expected
            deriv = coeff*5 + "X^4 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:25: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:29: class, interface, or enum expected
            equation = equation + coeff + "X^4 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:30: class, interface, or enum expected
            deriv = deriv + coeff*4 + "X^3 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:31: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:35: class, interface, or enum expected
            equation = equation + coeff + "X^3 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:36: class, interface, or enum expected
            deriv = deriv + coeff*3 + "X^2 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:37: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:41: class, interface, or enum expected
            equation = equation + coeff + "X^2 + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:42: class, interface, or enum expected
            deriv = deriv + coeff*2 + "X + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:43: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:47: class, interface, or enum expected
            equation = equation + coeff + "X + ";
            ^
H:\Derivative quiz\derivativeQuiz.java:48: class, interface, or enum expected
            deriv = deriv + coeff;
            ^
H:\Derivative quiz\derivativeQuiz.java:49: class, interface, or enum expected
        }
        ^
H:\Derivative quiz\derivativeQuiz.java:53: class, interface, or enum expected
            equation = equation + coeff;
            ^
H:\Derivative quiz\derivativeQuiz.java:54: class, interface, or enum expected

            if(deriv == "")
            ^
H:\Derivative quiz\derivativeQuiz.java:57: class, interface, or enum expected
            }
            ^
H:\Derivative quiz\derivativeQuiz.java:114: class, interface, or enum expected
    JOptionPane.showMessageDialog(null, "Question " + z + "\\" + question + "\nDerivative: " + deriv);
    ^
H:\Derivative quiz\derivativeQuiz.java:115: class, interface, or enum expected
    }
    ^
33 errors

Process completed.

我觉得这是一个基本的错误,可是我似乎无法找到它。 如果它的确与众不同,我使用JCreator的编译和安装是否正确。

更新:我有固定参与的错误(类声明和不正确的import语句(一个人回去,并删除一些分号))

工作代码:

import java.util.Random;
import javax.swing.JOptionPane;
import java.lang.String;
public class derivativeQuiz_source{
public static void main(String args[])
{
    //a bunch more code
}
}

感谢所有帮助

Answer 1:

你错过了类的声明。

public class DerivativeQuiz{
   public static void derivativeQuiz(String args[]){ ... }
}


Answer 2:

每个方法都应该是一个类中。 你的方法derivativeQuiz是一类外。

public class ClassName {

 ///your methods
}


Answer 3:

你忘了你的类声明:

public class MyClass {
...


Answer 4:

看看你的函数的定义。 如果忘记使用“()”后函数声明的地方,你会得到很多格式相同的错误:

 ... ??: class, interface, or enum expected ...

而且你已经忘了你的类或函数定义结束后闭架。 但需要注意的是,这些丢失的支架,是不是这种类型的错误的唯一原因。



Answer 5:

类,接口,或枚举预期

当import语句是小姐拼写上述错误甚至有可能。 一个适当的说法是“进口com.company.HelloWorld;”

如果错误而写代码/编辑它小姐写的“T com.company.HelloWorld;”

编译器会显示“类,接口,或枚举预期”



Answer 6:

主要的方法应该在你的类像这样的声明:

public class derivativeQuiz_source{
    // bunch of methods .....

    public static void main(String args[])
    {
        // code 
    }
}


文章来源: Compiler error: “class, interface, or enum expected”