Monday 15 March 2010

java - I'm having trouble writing static methods -



java - I'm having trouble writing static methods -

i'm not experienced in making static methods...i wanted practice , i'm having problems. i'm trying create programme input number , prints out squares less b. example, if set in 100, returns 0, 1, 4, 9, 16, 25, 36, 49, 64, 81. i'm getting errors, though.

illegal modifier parameter getsquares; final permitted. on line public static double getsquares(double b)

-the method getsquares(int) undefined type squares when seek squares.getsquares(100);...i'm guessing because of first problem. please help me, know static methods of import don't know how create them.

package testers; import java.util.scanner; public class squares { public static void main(string[] args) { squares.getsquares(100); public static double getsquares(double b) { double sqrtnum=math.sqrt(b); int i=0; while(i<sqrtnum) { sqrtnum=math.pow(i,2); system.out.print(sqrtnum+" "); i++; } } } }

you can't declare method within method - format code , clearer see. example:

package testers; import java.util.scanner; public class squares { public static void main(string[] args) { squares.getsquares(100); } public static double getsquares(double b) { double sqrtnum = math.sqrt(b); int = 0; while(i < sqrtnum) { sqrtnum = math.pow(i, 2); system.out.print(sqrtnum + " "); i++; } } }

also, there no returned value in getsquares() - looks intended create void. finally, while loop:

int = 0; while(i < sqrtnum) { // code i++; }

can simplified for loop:

for (int = 0; < sqrtnum; i++) { // code }

java

No comments:

Post a Comment