从if和else.Calee递归调用相同的方法获取意外的输出(从sysout中可以看到)。 预期产出:4
调试代码并看到发生了什么。但是不能理解。请帮忙
public class GetSquares {
/**
* @param args
*/
public static void main(String[] args) {
String rectDimension="5X3";
int noOfSquares=0;
int noOfSq=getNoofSquares(rectDimension,noOfSquares);
System.out.println(noOfSq);
}
public static int getNoofSquares(String rectDimension,int noOfSquares){
int length=Integer.parseInt(rectDimension.split("X")[0]);
int breadth=Integer.parseInt(rectDimension.split("X")[1]);
if(length<breadth){
rectDimension=String.valueOf(breadth-length)+"X"+length;
++noOfSquares;
getNoofSquares(rectDimension, noOfSquares);
}else if(length>breadth){
rectDimension=String.valueOf(length-breadth)+"X"+breadth;
++noOfSquares;
getNoofSquares(rectDimension, noOfSquares);
}else {
noOfSquares=noOfSquares+1;
System.out.println(noOfSquares);
return noOfSquares;
}
return noOfSquares;
}
}
答案 0 :(得分:0)
你没有在递归调用中保存方法getNoofSquare()
的返回值,
得到像
noOfSquares= getNoofSquares(rectDimension, noOfSquares);
答案 1 :(得分:0)
您的问题是Java中的原始类型是按值传递的,而不是通过引用传递的。
public static void increment(int x) {
x++;
}
public static void main(String... a) {
int x = 0;
increment(x);
System.out.println(x);
}
此代码将打印0
。
如果你想要一个“输出参数”,它必须是可变的,但int
不是。您必须创建一个类,该类是整数的持有者:
public class MutableInteger {
private int value = 0;
public void increment() {
value++;
}
public int getValue() {
return value;
}
}
然后你的代码看起来像这样:
// SOLUTION 1
public class GetSquares {
public static void main(String[] args) {
String rectDimension = "5X3";
MutableInteger noOfSq = new MutableInteger();
getNoofSquares(rectDimension, noOfSq);
System.out.println(noOfSq.getValue());
}
public static void getNoofSquares(String rectDimension, MutableInteger noOfSquares) {
int length = Integer.parseInt(rectDimension.split("X")[0]);
int breadth = Integer.parseInt(rectDimension.split("X")[1]);
noOfSquares.increment();
if (length < breadth) {
rectDimension = String.valueOf(breadth - length) + "X" + length;
getNoofSquares(rectDimension, noOfSquares);
} else if (length > breadth) {
rectDimension = String.valueOf(length - breadth) + "X" + breadth;
getNoofSquares(rectDimension, noOfSquares);
}
}
}
另一种方法是只使用返回值来返回结果:
// SOLUTION 2
public class GetSquares {
public static void main(String[] args) {
String rectDimension = "5X3";
int noOfSq = getNoofSquares(rectDimension);
System.out.println(noOfSq);
}
public static int getNoofSquares(String rectDimension) {
int length = Integer.parseInt(rectDimension.split("X")[0]);
int breadth = Integer.parseInt(rectDimension.split("X")[1]);
if (length < breadth) {
rectDimension = String.valueOf(breadth - length) + "X" + length;
return 1 + getNoofSquares(rectDimension);
} else if (length > breadth) {
rectDimension = String.valueOf(length - breadth) + "X" + breadth;
return 1 + getNoofSquares(rectDimension);
} else {
return 1;
}
}
}
此外,我建议只使用两个int
长度和宽度参数,而不是进行字符串解析/拆分/构建:
// SOLUTION 2 (cleaned)
public class GetSquares {
public static void main(String[] args) {
String rectDimension = "5X3";
int length = Integer.parseInt(rectDimension.split("X")[0]);
int breadth = Integer.parseInt(rectDimension.split("X")[1]);
int noOfSq = getNoofSquares(length, breadth);
System.out.println(noOfSq);
}
public static int getNoofSquares(int length, int breadth) {
if (length < breadth) {
return 1 + getNoofSquares(breadth - length, length);
} else if (length > breadth) {
return 1 + getNoofSquares(length - breadth, breadth);
} else {
return 1;
}
}
}
答案 2 :(得分:0)
Java是按值传递的。您正在递增一个局部变量,并且您没有对递归返回的值执行任何操作。
既然你没有做任何事情,但回来后你也可以做得很好:
public class GetSquares {
public static void main(String[] args) {
String rectDimension = args.length > 0 ? args[0] : "5X3";
System.out.println("Number of squares from " + rectDimension
+ " is " + getNoofSquares(rectDimension));
}
public static int getNoofSquares(String rectDimension){
String parts[] = rectDimension.split("X");
int length=Integer.parseInt(parts[0]);
int breadth=Integer.parseInt(parts[1]);
return getNoofSquares(length, breadth, 0);
}
public static int getNoofSquares(int length, int breadth, int noOfSquares){
if (length < breadth) {
return getNoofSquares(breadth-length, length, noOfSquares+1);
} else if (length > breadth) {
return getNoofSquares(length-breadth, breadth, noOfSquares+1);
} else {
return noOfSquares+1;
}
}
}
我保留了你的累加器所以现在它的尾部递归。但Java还没有TCO。