第四章 方法和数组 Fig 4.1 SquareInt.cs

本文介绍了一个使用C#编写的简单程序,该程序能够计算并显示1到10之间每个整数的平方值。通过按钮点击触发计算过程,并将结果显示在界面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SquareInt
{
     /// <summary>
     /// summary description fro SquareInt.
     /// </summary>
     public class SquareInt :System.Windows.Forms.Form
     {
        private System.Windows.Forms.Button calculateButton;
        private System.Windows.Forms.Label outputLabel;
        ///<summary>
        ///Required descriptioin variable
        ///</summary>
        private System.ComponentModel.Container components = null;
       
        public SquareInt()
        {
             //
             // required for windows form designer support
             //
             InitializeComponent();
        }
        /// <summary>
        ///Clean up any resource being used
        ///</summary>
        protected override void Dispose( bool disposing )
        {
           if(disposing)
           {
              if( components != null )
              {
                components.Dispose();
             
              }
          
           }
           base.Dispose(disposing);
          
        }
     # region Windows Form Designer generated code
     
     ///<summary>
     ///required method for description of suport
     ///</summary>
     private  void InitializeComponent()
     {
         this.calculateButton =
            new System.Windows.Forms.Button();
        this.outputLabel = new System.Windows.Forms.Label();
        this.SuspendLayout();
        //
        // calculateButton
        //
        this.calculateButton.Location =
           new System.Drawing.Point(32, 8);
        this.calculateButton.Name = "CalculateButton";
        this.calculateButton.Size =
           new System.Drawing.Size(120, 23);
        this.calculateButton.TabIndex = 0;
        this.calculateButton.Text ="Calculate Squares";
        this.calculateButton.Click +=
          new System.EventHandler(this.calculateButton_Click);
        //
        //outputLable
        //
        this.outputLabel.Location=
           new System.Drawing.Point(16, 48);
        this.outputLabel.Name ="outputLable";
        this.outputLabel.Size =
           new System.Drawing.Size(152,144);
        this.outputLabel.TabIndex = 1;
        //
        // Square Int
        //
        this.AutoScaleBaseSize =
           new System.Drawing.Size(5, 13);
        this.ClientSize =
           new System.Drawing.Size(184,197);
        this.Controls.AddRange(
           new System.Windows.Forms.Control[] {
              this.outputLabel,
              this.calculateButton});
        this.Name = "SquareInt";
        this.Text = "SquareInt";
        this.ResumeLayout(false);
       
     }
    #endregion
    ///<summary>
    ///the main entry point for the application
    ///</summary>
   
    [STAThread]
    static void Main()
    {  
       Application.Run(  new SquareInt());
    }
    // Square method definition
   
    int Square( int y)
    {
       return y*y;
    }
   
    private void calculateButton_Click(
       object sender , System.EventArgs e)
       {  
          outputLabel.Text = "";
         
          // loop 10 times
          for (int counter = 1; counter <= 10; counter++)
          {
             int result = Square(counter);
             
             outputLabel.Text += " The square of " + counter + "is " +result + "/n";
           
          }
        
    }
   
   
   
     }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值