web-dev-qa-db-fra.com

Erreur du compilateur: "classe, interface ou énumération attendue"

J'ai dépanné ce programme pendant des heures, essayé plusieurs configurations et je n'ai pas eu de chance. Il a été écrit en Java et comporte 33 erreurs (contre 50 auparavant)

Code source:

/*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
}

Le journal des erreurs (compilé dans 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.

J'ai l'impression que c'est une erreur fondamentale, et pourtant je n'arrive pas à la trouver. Si cela fait une différence, j'utilise JCreator pour compiler et tout est installé correctement.

UPDATE: J'ai corrigé les erreurs impliquées (déclaration de classe et instructions d'importation incorrectes (quelqu'un est revenu et a supprimé quelques points-virgules))

Code de travail:

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
}
}

Merci pour votre aide

16
Azulflame

Vous manquez la déclaration de classe.

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

Chaque méthode devrait être dans une classe. Votre méthode derivativeQuiz est en dehors d'une classe.

public class ClassName {

 ///your methods
}
8
Suraj Chandran

Vous avez oublié votre déclaration de classe:

public class MyClass {
...
4
jalynn2

classe, interface ou énumération attendue

L'erreur ci-dessus est même possible lorsque l'instruction d'importation est manquante. Une déclaration appropriée est "import com.company.HelloWorld;"

Si, par erreur, lors de l’écriture/de la modification du code, il est écrit comme "t com.company.HelloWorld;"

le compilateur affichera "classe, interface ou énumération attendue"

0
Prakhyat

Regardez la définition de votre fonction. Si vous oubliez d'utiliser "()" après la déclaration de la fonction quelque part, vous obtiendrez de nombreuses erreurs avec le même format:

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

Et vous avez également oublié de fermer le crochet après la fin de la définition de votre classe ou fonction. Mais notez que ces parenthèses manquantes ne sont pas la seule raison de ce type d'erreur.

0
Erfankam