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 に答える