0
Can someone please help me?
I'am trying to figure out whats the error, but I can't find it. Here is the code: public class Program { public static void main(String[] args) { public double absoluteValue(double x) { if(x < 0) { return -x; } else { return x; } } } } PLEASE HELP!
7 Antworten
+ 3
You have created a function inside of main function make it outside and than call it. Like
public class Program
{
public static void main(String[] args) {
Program obj = new Program();
System.out.println(obj.absoluteValue(-4.8));
}
public double absoluteValue(double x)
{
if(x < 0) {
return -x;
} else {
return x;
}
}
}
+ 3
btw aditya first one 😇
+ 2
You defined the method absolute Value inside main () method.
I don't think we can define a method like this. Yes we can define anonymous method for event handlers, but this is not possible as far as I know.
Hope this will rectify your issue
+ 2
oh oh 3 answers @ same time 😃
+ 2
Actually, I am first😅
+ 1
you cant declare a function within another function declaration.
public class Program
{
public static void main(String[] args) {
System.out.println(absoluteValue(3.5));
}
public static double absoluteValue(double x) {
if(x < 0) {
return -x;
} else {
return x;
}
}
}
+ 1
Thanks guys