public class Test { public void sub(int Max, int n) { if (n > Max || n == 0)
return;
else {
for (int i = 0; i < n; i++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * (Max - n) + 1; j++) {
System.out.print("*");
}
System.out.println();
this.sub(Max, n - 1);
}
} public void sub(int n) {
sub(n, n);
} public static void main(String[] args) {
Test t = new Test();
t.sub(10);
}}

解决方案 »

  1.   

    public class Test { public void sub(int Max, int n) { if (n > Max || n == 0)
    return;
    else {
    for (int i = 0; i < n; i++) {
    System.out.print(" ");
    }
    for (int j = 0; j < 2 * (Max - n) + 1; j++) {
    System.out.print("*");
    }
    System.out.println();
    this.sub(Max, n - 1);
    }
    } //public void sub(int n) {
    // sub(n, n);
    //} public static void main(String[] args) {
    Test t = new Test();
    t.sub(10,10);
    }}==================================
    为什么不直接t.sub(10,10);呢???!
      

  2.   

    为了让你的程序用起来方便...如果有人好奇的输入:
    t.sub(10,5);那么他只能看到5-10之间的行,也许会造成用户的困扰..你的需求就是要从上到下依次输入一个“*”,三个“*”...也许你并不想知道Max是什么意思..而如果我还把Max和n来让你填的话就不能达到“封装”的目的了..虽然有灵活性。
      

  3.   

    回复: "skylovers(琴声刺骨(不再为女人而伤心)) "
    不错,用到了真正的面向对像编程/;;;
      

  4.   

    class sub
      {  int fact(int n)//返回int?下面的输出怎么办?还有就是前面最好加上3P(public,protected,private)..不然很容易出问题..虽然默认是protected...
           
      }
      
    public class Test
     {  public static void main(String args[])
          { sub subfac=new sub();
            int x;//如果你以后不想再用到x,那么你最好把他在for里面定义
            for(x=1;x<=7;x+=2)
               { if(x==8) continue;//此行不必要,因为x永远不会为8
                 System.out.println(x+"!= "+subfac.fact(x));
    /*没有看出来什么意思...难道想逐行输出?可是你的fact()返回的是一个int值....最终的输出结果应该是类似:"1!=5"*/
               }
          }
     }      
    还有就是按行输出的话,前面的空格不好确定有多少个...除非是已经规定死我只输出多少行...至于如何递归...不是不愿改,而是小弟太笨...