问题描述
我正在尝试通过递归在 Java 中构建一个简单的棋盘布局,但我的问题是,在每次递归调用中,字段的数量都会减少一个,这是错误的。黑色字段用“#”表示,白色字段用空格表示。我已经用 Iteration 完成了这件事,这没问题,但递归方法让我头疼。
import java.util.Scanner;
public class Chess {
public static int chess(int boardlength) {
if (boardlength == 0) {
return boardlength;
} else {
pattern(boardlength);
System.out.println("\n");
}
return chess(boardlength - 1);
}
public static int pattern(int runs) {
if (runs == 0) {
return runs;
} else {
if (runs % 2 != 0) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
return pattern(runs - 1);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Length of the board: ");
int boardlength = input.nextInt();
chess(boardlength);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)