Java 如何以正确的方式打印点图案?
这就是我所拥有的 当我在makePattern(int-size)中省略一个makePattern(size-1)时,我得到了下半部分。但我不知道如何得到上半场Java 如何以正确的方式打印点图案?,java,Java,这就是我所拥有的 当我在makePattern(int-size)中省略一个makePattern(size-1)时,我得到了下半部分。但我不知道如何得到上半场 public void makePattern(int size){ stringList = new ArrayList<String>(); if (size == 0){ System.out.print(""); } else{ makePatter
public void makePattern(int size){
stringList = new ArrayList<String>();
if (size == 0){
System.out.print("");
}
else{
makePattern(size - 1);
System.out.println(dotString(size));
makePattern(size - 1);
}
}
测试的主要方法:
public static void main(String[] args) {
Pattern rP = new Pattern();
int sizePt = 5;
System.out.println();
for(int i=1; i<=sizePt; i++) {
System.out.println("pattern " + i);
rP.makePattern(i);
ArrayList<String> pattern = rP.getStringList();
for(int j = 0; j < pattern.size() ; j++)
System.out.println(pattern.get(j));
System.out.println();
}
模式2
.
..
.
模式3
.
..
.
...
.
..
.
我想要的输出是:
i、 e.市场模式(3)
每次进行递归调用时,每次都会生成完整的模式。这不是你想要的。相反,您希望在每个函数调用中使模式的减半。上半部分或下半部分
因此,最终您需要一个递归函数来实现这一点:
.
..
...
一个递归函数可以实现这一点(可以用同一个函数同时实现)
以及一个将它们连接在一起的函数
您可以采取的另一种方法是每个函数只有一个递归调用 创建一个函数,用于在此图案上打印变体
...
....
.....
....
...
然后像这样称呼它
makePattern(**insert args here**)
{
//handle base case
System.out.println(dotString(size));
makePattern(**insert args here**)
System.out.println(dotString(size));
}
我的2美分:
public void makePattern(int size, int direction)
{
if (size == 0)
return; // quit recursion when we're down to 0
// start out recursively calling back until we reach 0 with -1 direction
// that will print AFTER the calls to the negative direction so the last
// recursive call prints FIRST
if (direction <= 0)
makePattern(size - 1, -1);
// all the makePatterns and prints for the lower direction will happen
// and return back to here where we call print for the original number
// passed
System.out.println(dotString(size));
// now we recursively call with direction = 1 AFTER printing so the last
// recursive call prints LAST
if (direction >= 0)
makePattern(size - 1, 1);
}
public void makePattern(整数大小,整数方向)
{
如果(大小==0)
return;//当我们降到0时退出递归
//开始递归回调,直到到达0,方向为-1
//这将在调用后打印到负方向,因此最后
//递归调用首先打印
如果(方向=0)
makePattern(大小-1,1);
}
您需要递归地执行此操作吗?这不是直接相关的,但应该会有帮助:我为java初学者编写了一个。关于打印的部分可能会帮助您理解代码的流程。“请帮助”不是一个问题。凯瑟非常礼貌地建议“学习如何调试”,这可能是您现在能得到的最好的帮助。一般来说,有比StackOverflow更好的调试技术。我希望在不循环的情况下递归地进行调试。@Pshemo-oops,我添加了psudocode。我还得到+1吗?我不知道如何开始另一个half@user3792115您可以直接从main方法或从非递归的helper函数生成这两个函数
...
..
.
...
....
.....
....
...
makePattern(**insert args here**)
{
//handle base case
System.out.println(dotString(size));
makePattern(**insert args here**)
System.out.println(dotString(size));
}
public void makePattern(int size, int direction)
{
if (size == 0)
return; // quit recursion when we're down to 0
// start out recursively calling back until we reach 0 with -1 direction
// that will print AFTER the calls to the negative direction so the last
// recursive call prints FIRST
if (direction <= 0)
makePattern(size - 1, -1);
// all the makePatterns and prints for the lower direction will happen
// and return back to here where we call print for the original number
// passed
System.out.println(dotString(size));
// now we recursively call with direction = 1 AFTER printing so the last
// recursive call prints LAST
if (direction >= 0)
makePattern(size - 1, 1);
}