web-dev-qa-db-ja.com

コンパイラエラー:「クラス、インターフェイス、または列挙型が必要です」

私は何時間もこのプログラムのトラブルシューティングを行い、いくつかの構成を試しましたが、運がありませんでした。 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
}
}

すべての助けてくれてありがとう

16
Azulflame

クラス宣言を見逃しています。

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

すべてのメソッドはクラス内にある必要があります。メソッドderivativeQuizはクラスの外部にあります。

public class ClassName {

 ///your methods
}
8
Suraj Chandran

クラス宣言を忘れました:

public class MyClass {
...
4
jalynn2

クラス、インターフェイス、または列挙型が必要です

上記のエラーは、importステートメントのスペルが間違っている場合でも発生する可能性があります。適切なステートメントは「import com.company.HelloWorld;」です。

コードの作成/編集中に誤って「t com.company.HelloWorld;」のように誤って記述された場合

コンパイラは「クラス、インターフェイス、または列挙型が期待されます」を表示します

0
Prakhyat

関数の定義を見てください。どこかで関数宣言の後に「()」の使用を忘れた場合、同じ形式で多くのエラーが発生します。

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

また、クラスまたは関数の定義が終了した後、閉じ括弧を忘れていました。しかし、このタイプのエラーの唯一の理由は、これらの欠落したブラケットではないことに注意してください。

0
Erfankam