C#學習筆記01:a+b=c 控制臺程序/窗體應用程序
練習:連續(xù)輸入一個數(shù)值a,
? ? ? ? ? 輸入一個數(shù)值b,計算a+b的結果。
? ? ? ? ? 用C#控制臺程序和窗體應用程序分別實現(xiàn)。
1.控制臺程序,實現(xiàn)計算a+b=c
using System;
namespace add
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int a;
? ? ? ? ? ? int b;
? ? ? ? ? ? int c;
? ? ? ? ? ? Console.Write("請輸入一個數(shù)據(jù)a:");//輸入一個數(shù)據(jù)a
? ? ? ? ? ? a =Convert.ToInt32( Console.ReadLine());
? ? ? ? ? ? Console.Write("請輸入一個數(shù)據(jù)b:");//輸入一個數(shù)據(jù)b
? ? ? ? ? ? b = Convert.ToInt32(Console.ReadLine());
? ? ? ? ? ? c = a + b;
? ? ? ? ? ? Console.WriteLine("a+b="+c);//計算a+b
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}
————————————————————————————————
2.窗體應用程序,實現(xiàn)計算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)
? ? ? ? {
? ? ? ? }
? ? }
}