Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

java - Compiler error: "class, interface, or enum expected"

I have been troubleshooting this program for hours, trying several configurations, and have had no luck. It has been written in java, and has 33 errors (lowered from 50 before)

Source Code:

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

The error log (compiled in JCreator):

--------------------Configuration: <Default>--------------------
H:Derivative quizderivativeQuiz.java:4: class, interface, or enum expected
public static void derivativeQuiz(String args[])
              ^
H:Derivative quizderivativeQuiz.java:9: class, interface, or enum expected
    int maxCoef = 15;
    ^
H:Derivative quizderivativeQuiz.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 quizderivativeQuiz.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 quizderivativeQuiz.java:12: class, interface, or enum expected
    Random random = new Random();
    ^
H:Derivative quizderivativeQuiz.java:13: class, interface, or enum expected
    int coeff;
    ^
H:Derivative quizderivativeQuiz.java:14: class, interface, or enum expected
    String equation = "";
    ^
H:Derivative quizderivativeQuiz.java:15: class, interface, or enum expected
    String deriv = "";
    ^
H:Derivative quizderivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
    ^
H:Derivative quizderivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                   ^
H:Derivative quizderivativeQuiz.java:16: class, interface, or enum expected
    for(int z = 0; z <= question; z++)
                                  ^
H:Derivative quizderivativeQuiz.java:19: class, interface, or enum expected
        deriv = "";
        ^
H:Derivative quizderivativeQuiz.java:20: class, interface, or enum expected
        if(numExp >= 5)
        ^
H:Derivative quizderivativeQuiz.java:23: class, interface, or enum expected
            equation = coeff + "X^5 + ";
            ^
H:Derivative quizderivativeQuiz.java:24: class, interface, or enum expected
            deriv = coeff*5 + "X^4 + ";
            ^
H:Derivative quizderivativeQuiz.java:25: class, interface, or enum expected
        }
        ^
H:Derivative quizderivativeQuiz.java:29: class, interface, or enum expected
            equation = equation + coeff + "X^4 + ";
            ^
H:Derivative quizderivativeQuiz.java:30: class, interface, or enum expected
            deriv = deriv + coeff*4 + "X^3 + ";
            ^
H:Derivative quizderivativeQuiz.java:31: class, interface, or enum expected
        }
        ^
H:Derivative quizderivativeQuiz.java:35: class, interface, or enum expected
            equation = equation + coeff + "X^3 + ";
            ^
H:Derivative quizderivativeQuiz.java:36: class, interface, or enum expected
            deriv = deriv + coeff*3 + "X^2 + ";
            ^
H:Derivative quizderivativeQuiz.java:37: class, interface, or enum expected
        }
        ^
H:Derivative quizderivativeQuiz.java:41: class, interface, or enum expected
            equation = equation + coeff + "X^2 + ";
            ^
H:Derivative quizderivativeQuiz.java:42: class, interface, or enum expected
            deriv = deriv + coeff*2 + "X + ";
            ^
H:Derivative quizderivativeQuiz.java:43: class, interface, or enum expected
        }
        ^
H:Derivative quizderivativeQuiz.java:47: class, interface, or enum expected
            equation = equation + coeff + "X + ";
            ^
H:Derivative quizderivativeQuiz.java:48: class, interface, or enum expected
            deriv = deriv + coeff;
            ^
H:Derivative quizderivativeQuiz.java:49: class, interface, or enum expected
        }
        ^
H:Derivative quizderivativeQuiz.java:53: class, interface, or enum expected
            equation = equation + coeff;
            ^
H:Derivative quizderivativeQuiz.java:54: class, interface, or enum expected

            if(deriv == "")
            ^
H:Derivative quizderivativeQuiz.java:57: class, interface, or enum expected
            }
            ^
H:Derivative quizderivativeQuiz.java:114: class, interface, or enum expected
    JOptionPane.showMessageDialog(null, "Question " + z + "" + question + "
Derivative: " + deriv);
    ^
H:Derivative quizderivativeQuiz.java:115: class, interface, or enum expected
    }
    ^
33 errors

Process completed.

I feel like this is a basic error, and yet I can't seem to find it. If it makes a difference, I am using JCreator to compile and everything is installed correctly.

UPDATE: I have fixed the errors involved (Class declaration and incorrect import statements (someone went back and deleted a few semicolons))

Working code:

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

Thanks for all the help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You miss the class declaration.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...