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!

25th Nov 2016, 12:30 PM
Teo
Teo - avatar
7 Respostas
+ 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; } } }
25th Nov 2016, 12:42 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
btw aditya first one 😇
25th Nov 2016, 12:47 PM
Baraa AB
Baraa AB - avatar
+ 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
25th Nov 2016, 12:43 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
oh oh 3 answers @ same time 😃
25th Nov 2016, 12:45 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 2
Actually, I am first😅
25th Nov 2016, 12:46 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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; } } }
25th Nov 2016, 12:44 PM
Roland
+ 1
Thanks guys
25th Nov 2016, 1:13 PM
Teo
Teo - avatar