在Applet中显示0至10的阶乘

1.显示结果

2.代码

import java.awt.*;
import java.applet.*;

//*********Found********
public class Java_2 extends Applet
{
    TextArea outputArea;

    public void init()
    {
        setLayout(new BorderLayout());
        outputArea = new TextArea();
     //*********Found********
        add( outputArea );

      // 计算0至10的阶乘
        for ( long i = 0; i <= 10; i++ )
            //*********Found********
            outputArea.append(i + "! = " +factorial(i)+ "\n" );
    }
   
   // 用递归定义阶乘方法
    public long factorial( long number )
    {                  
        if ( number <= 1 )  // 基本情况
            return 1;
        else
            //*********Found********
            return number * factorial(number- 1 );
    }  
}
发布了90 篇原创文章 · 获赞 50 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/qq_40307919/article/details/96986303
今日推荐