Saturday 15 September 2012

Java Recursion and Returns -



Java Recursion and Returns -

we aren't doing much recursion in java course, wanted larn wrote little test program. problem programme after getlength or getwidth methods recalled recursion, not utilize user's input in homecoming statements, resulting in displayarea method returning area 0.0. if point out error and/or solution, helpful. below code:

package recursiontest; import java.util.scanner; public class recursiontest { static void main(string[] args) { // todo code application logic here double length = getlength(); double width = getwidth(); double area = getdimensions(length, width); displayarea(area); } public static double getlength(){ double length = 0; scanner in = new scanner(system.in); system.out.print("please come in rectangle's length: "); if(in.hasnextdouble()){ length = in.nextdouble(); } else{ system.out.println("input must double."); getlength(); } homecoming length; } public static double getwidth(){ double width = 0; scanner in = new scanner(system.in); system.out.print("please come in rectangle's width: "); if(in.hasnextdouble()){ width = in.nextdouble(); } else{ system.out.println("input must double."); getwidth(); } homecoming width; } public static double getdimensions(double length, double width){ double area = 0; area = length * width; homecoming area; } public static void displayarea(double area){ system.out.println("area = "+area); } }

in getlength(), phone call recursively getlength(), don't utilize result, means return length still returns 0

replace getlength();

length = getlength();

(same width)

java recursion area

No comments:

Post a Comment