C#学习笔记01:a+b=c 控制台程序/窗体应用程序
练习:连续输入一个数值a,
输入一个数值b,计算a+b的结果。
用C#控制台程序和窗体应用程序分别实现。
1.控制台程序,实现计算a+b=c
using System;
namespace add
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
Console.Write("请输入一个数据a:");//输入一个数据a
a =Convert.ToInt32( Console.ReadLine());
Console.Write("请输入一个数据b:");//输入一个数据b
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
Console.WriteLine("a+b="+c);//计算a+b
Console.ReadLine();
}
}
}
————————————————————————————————
2.窗体应用程序,实现计算a+b=c
新建窗体应用程序,放置控件。三个TextBox,一个Button按钮。

控件放置好,修改输入框名称



using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ab01
{
public partial class Form1 : Form
{
int a;
int b;
int c;
public Form1()
{
InitializeComponent();
}
private void dataB_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(dataA.Text);
b= Convert.ToInt32(dataB.Text);
c = a + b;
dataC.Text = Convert.ToString(c);
}
private void dataA_TextChanged(object sender, EventArgs e)
{
}
private void dataC_TextChanged(object sender, EventArgs e)
{
}
}
}