递归经典面试题_ 小例

主要内容如下:

 1         private void button1_Click(object sender, EventArgs e)
 2         {   
 3             //按钮事件
 4             int value;
 5             if(int.TryParse(textBox1.Text,out value)){
 6                 label2.Text= function(value).ToString();
 7             }else{
 8                 MessageBox.Show("请输入正确的数","提示:");
 9             }
10             
11         }
12         //定义递归计算的方法
13         int function(int args)
14         {
15             if(args==0){
16                 return 0;
17             }else if(args>=1&&args<=2){
18                 return 1;
19             }else{
20                 return function(args - 1) + function(args - 2);
21             }
22         }

实现结果:

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/9858889.html
今日推荐