2012-07-26 52 views
3

我正在做什么应该是一个相对简单的Java递归问题,但我似乎无法找到一个简单的,单方法的解决方案。递归java星号

我试图以递减之后升序打印出星号,这样当用户在3通过例如,打印出来会是这样的:

* 
** 
*** 
** 
* 

编辑:谢谢你的帮助的@dasblinkenlight这已演变为:

public void patternMaker(int start, int max, int direction){ 
    if(start == 0){ 
     return; 
    } 
    for(int i = 0; i < start; i++){ 
     System.out.print("*"); 
    } 
    System.out.println(); 
    if(start == max){ 
     direction = -1; 
    } 
    patternMaker(start + direction, max, direction); 

现在,它打印星号的正确数量,并以正确的顺序:

* 
** 
*** 
** 
* 

感谢大家的帮助!

+0

Psssst,答案有方向,但如果您遇到问题,或只是好奇,我把一个解决方案在http://ideone.com/StJRS :) – 2012-07-26 04:35:32

+0

@RayToal,感谢帮助,但我不确定他是否希望我们使用Character包装类或导入任何特定的包。 – ohayon 2012-07-26 14:53:30

+0

确实,那只是为了好玩 - *眨眼*很高兴你注意到!当然,使用循环 - 肯定比我在那里扔的单行恐怖更快,更容易理解。 :) – 2012-07-26 15:38:25

回答

3

你需要另一个参数来告诉你你要走哪条路。您还需要测试结束条件,以了解何时从上升到下降。

public void patternMaker(int x, int direction){ 
    // Direction is either +1 or -1, depending on whether you go up or down 
    // at the moment. Once you reach 3, switch the sign; 
    // Once you reach zero, stop. 

    // Pseudocode: 
    // If x is zero, we're done 
    // Print x asterisks followed by newline 
    // if x == 3, make direction -1 
    // Perform the recursive call with (x+direction, direction) 
} 

在递归之前绘制恒星可能会更容易,尽管这两种方法都是可行的。

+0

有意思,谢谢回复!我会给这个镜头和霍勒回来。 – ohayon 2012-07-26 04:19:49

+0

所以我很喜欢这种方法,虽然我似乎无法完全实现它。我现在通过在递归调用之前和之后使用循环来双向打印(仍然有一个参数),但是它的打印方式与我想要的方式相反。我不太确定如何使用stackoverflow向您展示我的当前代码并再次打印出来,而不会发布其他问题,因此我将在此处发布链接。 – ohayon 2012-07-26 14:45:58

+0

我刚才发现我应该编辑我原来的问题,所以修改如上。非常感谢! – ohayon 2012-07-26 14:52:04

4

我不知道你可以递归一个参数。我认为你需要两个。这里的想法:

stars(2, 4) 

将打印

** 
*** 
**** 
*** 
** 

stars(5, 6) 

将打印

***** 
****** 
***** 

这自然是递归的。为什么?因为

stars(n, n) 

只是打印n颗星。这是基本情况。现在递归步骤怎么样?那么让我们试试吧

stars(k, n) 

其中k < n。这是这样做的

draw a line of k stars 
call stars(k + 1, n) 
draw a line of k stars 

就是这样。当然,有一个支票是好的,但我们相信你可以弄清楚。

+0

感谢您的回应,我相信递归可以只用一个参数完成,但我不确定是否知道如何在最后实现该伪代码。 – ohayon 2012-07-26 04:38:31

+0

如果你有一个参数,让我们都知道。如果您最终需要关于伪代码的帮助,请参阅我的完整工作Java解决方案,网址为http://ideone.com/StJRS – 2012-07-26 04:55:44

1
static String patternMaker(int x, String result){ 
    String curStr =""; 
    for (int i=0; i<x; i++) 
     curStr += "*"; 
    curStr += "\n"; 
    if (result == null){    
     return patternMaker(x-1,curStr); 
    }else if (x > 0){   
      return patternMaker(x-1, curStr+result+curStr); 
    } 
    else 
     return result; 
} 

//test with : 
System.out.println(patternMaker(3,null)); 
+0

这是我从一开始就希望采用的路线。现在测试。谢谢! – ohayon 2012-07-26 04:43:07

-1
public class pattern { 

public void patternMaker(int x){ 
    if(x > 0){ 
     for(int i = 0; i < x; i++){ 
      for(int j=0;j<=i;j++){ 
       System.out.print("*"); 
      } 
      System.out.print("\n"); 
     } 
     for(int i = x-1; i > 0; i--){ 
      for(int j=i;j>0;j--){ 
       System.out.print("*"); 
      } 
      System.out.print("\n"); 
     } 
    } 
} 
public static void main(String[] ar){ 
    new pattern().patternMaker(3); 
} 
} 
+1

问题是关于递归......看起来你错过了那部分。 – SiB 2012-07-26 08:02:43