实验内容

  1. 分别采用控制台、Windows窗体、Web应用程序实现Hello World应用程序,熟悉Visual C#环境;
  2. 实现“装箱”和“拆箱”程序;
  3. 用两种方式编写“百元百鸡”程序。

1. “Hello World!”

(1)控制台输出显示HelloWorld

1
2
3
4
5
6
7
8
9
10
11
12
using System;

namespace HW_Console
{
   class Program
  {
       static void Main(string[] args)
      {
           Console.WriteLine("Hello World!");
      }
  }
}

image-20220213004259010

(2)Windows窗体输出显示HelloWorld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
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 HW_Windows
{
   public partial class Form1 : Form
  {
       public Form1()
      {
           InitializeComponent();
      }

       private void button1_Click(object sender, EventArgs e)
      {
           MessageBox.Show("Hello World!");
      }
  }
}

image-20220213004310013

(3)Web显示HelloWorld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HW_Web
{
   public partial class HW_Web : System.Web.UI.Page
  {
       protected void Page_Load(object sender, EventArgs e)
      {

      }

       protected void Button1_Click(object sender, EventArgs e)
      {
           Label1.Text = "Hello World!";       }
  }
}

image-20220213004318307

2. 装箱、拆箱操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

namespace Box
{
   class Program
  {
       static void Main(string[] args)
      {
           int x = 123, y;
           //装箱
           object obj1=x;
           //拆箱
           y = (int)obj1;
           Console.WriteLine("y={0}",y);
      }
  }
}

image-20220213004331826

3. “百元百鸡”两种写法

(1)百元百鸡(将买鸡的数量设为变量)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;

namespace HDHC1
{
   class Program
  {
       //百元白鸡算法1:设x,y,z为公鸡母鸡小鸡数
       //5x+3y+z/3=100
       //x+y+z=100
       //z%3=0
       static void Main(string[] args)
      {
           
           int x;
           int y= 0;
           int z=0;
        for(x=0;x<=20;x++)
               for(y=0;y<=100/3;y++)
              {
                   z = 100 - x - y;
                   if ((5 * x + 3 * y + z / 3 == 100) && (z%3==0))
                       Console.WriteLine("{0}只公鸡,{1}只母鸡,{2}只小鸡", x, y, z);
              }
           
      }

image-20220213004339769

(2)百元百鸡(将买鸡的钱设为变量)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;

namespace HDHC2
{
   class Program
  {
       //百元白鸡算法2:分别设x,y,z为买公鸡、母鸡和小鸡的钱
       //x/5+y/3+3z=100
       //x+y+z=100
       //x%5=0 y%3=0
       static void Main(string[] args)
      {

           int x;
           int y = 0;
           int z = 0;
           for (x = 0; x <= 100; x++)
               for (y = 0; y <= 100; y++)
              {
                   z = 100 - x - y;
                   if((x/5+y/3+3*z==100)&&(x%5==0)&&(y%3==0))
                       Console.WriteLine("{0}只公鸡,{1}只母鸡,{2}只小鸡", x/5, y/3, z*3);
              }

      }
  }
}

image-20220213004346707