首页 > Windows开发 > 详细

计算阶乘并显示_winform (20以后的阶乘溢出)

时间:2015-12-15 00:44:10      阅读:395      评论:0      收藏:0      [点我收藏+]

编写一个窗体应用程序,计算n的阶乘,显示其结果,同时,将结果显示在一个标签中。

新建窗体应用程序(如下),新建控件label1,label2,label3,textBOX1,button1,button2

技术分享

label1的Text属性改为“计算阶乘演示”

label2的Text属性改为“请输入需要的阶乘数”

button1的Text属性改为“确定”

button1的Text属性改为“退出”

技术分享

完整代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace 阶乘_winform
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19 
20         private void button2_Click(object sender, EventArgs e)//退出按钮代码
21         {
22             Application.Exit();
23         }
24 
25         private void button1_Click(object sender, EventArgs e)//确定按钮代码
26         {
27             try
28             {
29                 long num = Convert.ToInt32(textBox1.Text) + 1;//num的阶乘
30                 long product = 1;//乘积
31                 if (num != 0)//零的阶乘为一
32                 {
33                     long[] mylongArray = new long[num];//新建阶乘数组
34                     string temp;//临时存储数据
35                     mylongArray[0] = 1;//零的阶乘为一
36                     label3.Text = string.Format("{0}的阶乘是:{1} \r\n", 0, mylongArray[0]);//输出零的阶乘为一
37                     temp = label3.Text;
38                     for (long i = 1; i < num; i++)
39                     {
40                         product = product * i;
41                         mylongArray[i] = product;
42                         temp = temp + string.Format("{0}的阶乘是:{1} \r\n", i, mylongArray[i]);
43                     }
44                     label3.Text = temp;
45                 }
46                 else
47                 {
48                     label3.Text = "0的阶乘是:1";//零的阶乘为一
49                 }
50             }
51             catch
52             {
53                 MessageBox.Show("请输入正确的数字");
54             }
55         }
56 
57     }
58 }

 

计算阶乘并显示_winform (20以后的阶乘溢出)

原文:http://www.cnblogs.com/start-from-scratch/p/5046916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!