17

我已经对这个程序进行了几个小时的故障排除,尝试了几种配置,但都没有运气。它是用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 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
}
}

感谢所有的帮助

4

6 回答 6

27

你错过了类声明。

public class DerivativeQuiz{
   public static void derivativeQuiz(String args[]){ ... }
}
于 2012-10-05T16:08:05.757 回答
8

每个方法都应该在一个类中。您的方法derivativeQuiz在类之外。

public class ClassName {

 ///your methods
}
于 2012-10-05T16:07:48.207 回答
4

你忘记了你的班级声明:

public class MyClass {
...
于 2012-10-05T16:07:52.523 回答
0

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

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

而且您在类或函数定义结束后忘记了右括号。但请注意,这些缺少的括号,并不是此类错误的唯一原因。

于 2014-02-28T19:08:56.903 回答
0

需要类、接口或枚举

当 import 语句拼写错误时,甚至可能出现上述错误。正确的说法是“import com.company.HelloWorld;”

如果在编写/编辑代码时出错,它会像“t com.company.HelloWorld;”这样写错。

编译器将显示“预期的类、接口或枚举”

于 2015-03-28T11:06:47.350 回答
-2

main 方法应该像这样在你的类中声明:

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

    public static void main(String args[])
    {
        // code 
    }
}
于 2016-10-18T11:21:21.843 回答