Why do I get the error "Reached end of file while parsing"?
import javax.swing.JOptionPane;
class Overload
{
void average(int a, int b, int c)
{
average= (a+b+c)/3;
}
void average(int d, int e, double a, double b)
{
average= (d+e+a+b)/4;
}
void average(double c,double d,double e,double f,double g)
{
average= (c+d+e+f+g)/5;
}
class mainOverload
{
public static void main(String args[])
{
Overload object=new Overload();
object.average(7, 5, 1);
object.average(15, 12, 15.12, 12.15);
object.average(7.7, 8.4, 30.2, 1.4, 6.4);
}
}
You forgot to close the 3rd average
method:
void average(double c,double d,double e,double f,double g)
{
average= (c+d+e+f+g)/5;
} // << here
Regarding your comment - you haven't declare a variable called average
and yet you try to use it in each of the average methods. Declare this variable as a class field or a method field to solve this.
You are missing the closing brace of the method average
with 5 double
parameters.
This way the compiler interprets the class closing brace as the method closing brace, mainOverload
as a nested class and is missing the final closing brace for the Overload
class.
Because you forgot the closing bracket }
for the class Overload
You are missing a closing parenthesis for the
average(double c,double d,double e,double f,double g)
method. This should be
void average(double c,double d,double e,double f,double g)
{
average= (c+d+e+f+g)/5;
}//Missing!
A bracket is left here
{
average= (c+d+e+f+g)/5;
} // !!!
}
have this new ERROR. "cannot find symbol variable average"...
The problem is that you are assigning to a variable that you haven't declared.
void average(double c,double d,double e,double f,double g)
{
average= (c+d+e+f+g)/5; // <<<=== you haven't declared the 'average' variable.
}
But even if you do, your average
methods don't make a lot of sense. It would be better to declare the methods as returning the average or their arguments; e.g.
double average(double c,double d,double e,double f,double g)
{
return (c+d+e+f+g)/5;
}
and then call them like this (for example)
System.out.println(average(7.7, 8.4, 30.2, 1.4, 6.4));
oh man...java give me lot of error...im new in Java ... :(
Yup. You seem to be trying to learn by trial and error. This is a BAD IDEA. It will be slow and frustrating, and you will fail to recognize / understand important concepts and practices that are second nature to properly educated programmers.
You need to buy and read a Java textbook and do the exercises.
(For instance, your text book will explain how you can avoid having to write a bazillion different average
methods with different numbers and types of arguments.)
精彩评论