当前位置: 首页 > 面试题库 >

编译器错误:“预期的类,接口或枚举”

曾新
2023-03-14
问题内容

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

谢谢你的帮助


问题答案:

您错过了课堂宣讲。

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


 类似资料:
  • 问题内容: 我需要知道此代码的输出。但这不起作用。也许代码是错误的。我仍在学习如何使用Java,我尝试修复了几个小时,但还是没有运气。 这是代码: 谁能告诉我代码中有什么错误或遗漏吗? 问题答案: 例如: 另请注意,这可能无法打印您期望的结果。它实际上会打印: 为什么?构造函数总是链接到超类。

  • 我正在为插入排序算法运行下面的java代码,结果出现了一个错误:, 错误:java:类,接口,或枚举预期 当我删除主函数的静态关键字时,编译器不会向我显示运行选项。这里有什么问题吗?

  • 在下面的代码中,我将取消警告,因为我将使用不推荐使用的assertEquals方法。我遇到的问题是,当我运行下面的代码时,会出现错误: 我的测试。java:3:错误:类、接口或枚举需要导入组织。朱尼特*;

  • 问题内容: 我正在尝试在数据库中执行插入测试数据,但是无法正常工作。 将此视频作为参考,但无法正常运行:https : //www.youtube.com/watch?v=RPi7ueKwEXg 见下文: 问题答案: 当类声明中没有代码时,Android Studio会抛出该错误。您现在不在课堂上。在之后删除多余的右大括号,这应该可以解决。

  • 问题内容: 编写并运行此代码后,编译器将显示Expected Declaration错误: 问题答案: 之所以会出现此错误,是因为您在类中的错误位置放置了代码,因此将其移至任何函数或viewDidLoad方法中。 您只能在类范围内声明,而不能执行表达式。 它将正常工作。 编辑: 在您的第二个UIViewController只是通过NSUserDefaults这种方式阅读highScore : 因此

  • 一行代码有4个错误 读为“public action val actionPerform(ActionEvent事件){”的行获取“非法开始操作”两次和“;预期”两次。 我从第一本Java书中复制了这段代码,为什么它不能编译?