zoukankan      html  css  js  c++  java
  • 计算阶乘并显示_winform (20以后的阶乘溢出)

    编写一个窗体应用程序,计算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} 
    ", 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} 
    ", 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 }
  • 相关阅读:
    百度mp3地址解密码
    VB 在EXE后附加信息
    截屏函数
    Base64和StrToByte
    The Android ION memory allocator, DMABUF is mentioned as well
    DDC EDID 介绍
    Memory management for graphic processors TTM的由来
    科普 写display driver的必看 How video card works [2D的四种主要操作]
    GEM vs TTM
    DMABUF 背景介绍文章 Sharing buffers between devices
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/5046916.html
Copyright © 2011-2022 走看看