Topic: 递归调用的问题

  Print this page

1.递归调用的问题 Copy to clipboard
Posted by: dickran
Posted on: 2006-04-03 18:55

要求打迎出如下数据(使用递归调用)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

这是小弟我的程序

class A
{
public static int prints(int k)
{int i;
if(k!=0)
return
prints(k);

for(i=0;i<=k;i++)
System.out.print(i+" ");
System.out.println();
}
}

public class shuzi
{
public static void main(String args[])
{
A.prints(5);

}
}
亲请问这个程序正确吗

2.Re:递归调用的问题 [Re: dickran] Copy to clipboard
Posted by: why
Posted on: 2006-04-03 22:54

if(k!=0)
return
prints(k); //This is not 递归

for(i=0;i<=k;i++) // not 递归
System.out.print(i+" ");
System.out.println();


class A {
public static String prints(int k) {
if (k == 0) {
return "";
} else {
String s = prints(k-1) + " " + k;
System.out.println(s);
return s;
}
}
}
public class Shuzi { //classname should begin with an uppercase character by convention


OR

  public static String prints(int k) {
    String s;
    if (k == 1) {
      s = "1";
    } else {
      s = prints(k-1) + " " + k;
    }  
    System.out.println(s);
    return s;
  }


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923