Tuesday, July 28, 2009

Need java help?

why wont this work:


-----------------------------





public class chap5 {








public static void main(String[] args) {











System.out.print("Test: " + menu(5,10));





}

















public menu(int x, int y){








int c = x + y;





return c;








}








}

Need java help?
It means that you need to declare what menu should return.





public menu(int x, int y){


int c = x + y;


return c;


}





As it is, menu returns an integer. Therefore, to fix your error:





public int menu(int x, int y){


int c = x + y;


return c


}





You might need to convert the returned value to string to use with System.out.print()
Reply:First of all your menu method does not have return value





public int menu(int i, int y)





second, the call of method menu in the main method does not work because menu in this case does not belong to any class.





Two way to fix it, either make menu static


public static int menu(int i,int y)





or construct an object in the main method





public static void main(String args[])


{


System.out.println("Test:" + new chap5().menu(5,10));


}
Reply:In line 4 were it says(String[]args) it has to be (String args[])


swith the [] around and see if it works


No comments:

Post a Comment