C#类的应用实例1-石头剪刀布

C#类的应用非常广泛,可以用于各种软件开发项目,包括但不限于以下几个方面:

  1. 桌面应用程序开发:C#类可用于开发Windows桌面应用程序,如图形用户界面(GUI)应用程序、数据库应用程序等。通过定义类和对象,可以实现用户交互、数据存储和处理等功能。

  2. Web应用程序开发:C#类可以与ASP.NET框架结合使用,用于开发Web应用程序。通过定义类和对象,可以构建Web页面、处理用户请求、与数据库交互等。

  3. 游戏开发:C#类可用于开发游戏程序,如Unity3D游戏引擎中的脚本编写。通过定义类和对象,可以实现游戏角色控制、物理模拟、动画效果等功能。

  4. 移动应用程序开发:C#类可以与Xamarin平台结合使用,用于开发移动应用程序。通过定义类和对象,可以构建跨平台的移动应用程序,如Android和iOS平台上的应用程序。

  5. 数据库应用程序开发:C#类与ADO.NET技术结合使用,可以开发数据库应用程序。通过定义类和对象,可以与数据库进行连接、查询和操作数据等。

  6. 网络通信应用程序开发:C#类可以用于开发网络通信应用程序,如TCP/IP或UDP协议的网络应用程序。通过定义类和对象,可以实现客户端和服务器之间的数据交流和通信。

总的来说,C#类的应用范围非常广泛,可以用于各种类型的软件开发项目。通过定义类和对象,可以实现各种功能和业务逻辑,提高开发效率和代码重用性。

下面是个案例:

1、界面:

2、电脑类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 猜拳游戏
{
  
    public class computer
    {

        

        public string fist()
        {
            Random fist1 = new Random();
            int num = fist1.Next(0, 4);            
                switch (num)
            {
                case 1:
                    return "石头";
                    break;
                case 2:
                    return "剪刀";
                    break;
                default:
                    return "布";
                    break;
            }


        }

    }
}

2、裁判类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 猜拳游戏
{
    public enum result2
    {
        玩家赢,
            电脑赢,
            平手
    }

    public class judgment
    {
       public string playerresult { get; set; }
        public string computerresult { get; set; }
       /* public judgment(string playerresult)
        {
            this.playerresult = playerresult;
        }*/

        public  result2 judge(string playerresult,string computerresult)
        {
            if (playerresult=="石头" && computerresult=="石头")
            {
                return result2.平手;
            }
            else if (playerresult == "石头" && computerresult == "剪刀")
            {
                return result2.玩家赢;
            }
            else if (playerresult == "石头" && computerresult == "布")
            {
                return result2.电脑赢;
            }
            else if (playerresult == "剪刀" && computerresult == "石头")
            {
                return result2.电脑赢;
            }
            else if (playerresult == "剪刀" && computerresult == "剪刀")
            {
                return result2.平手;
            }
            else if (playerresult == "剪刀" && computerresult == "布")
            {
                return result2.玩家赢;
            }
            else if (playerresult == "布" && computerresult == "石头")
            {
                return result2.玩家赢;
            }
            else if (playerresult == "布" && computerresult == "剪刀")
            {
                return result2.电脑赢;
            }
            else
            {
               return result2.平手;

            }
                
        }


    }
}

3、玩家类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 猜拳游戏
{
   public class player
    {

        public string fistname { get; set; }

        public string  fist(string fistname)
        {
            switch(fistname)
            {
                case "石头":
                    return fistname; 
                    break;
                case "剪刀":
                    return fistname; 
                    break;
                case "布":
                    return fistname; 
                    break;
                default:
                    break;
            }
            return fistname;


        }
    }
}

4、窗口类

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 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

     

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;//等待

            this.Cursor = Cursors.Default;//正常状态
            Application.DoEvents(); //试试

        }
        private void bth_Rock_Click(object sender, EventArgs e)
        {
            NewMethod(bth_Rock.Text);
            NewMethod1();
            NewMethod2();

        }

        private void NewMethod2()
        {
            judgment thisgamejudgment = new judgment();
            labrefereeshowing.Text = thisgamejudgment.judge(labplayershowing.Text, labcomputershowing.Text).ToString();
        }

        private void NewMethod1()
        {
            computer thisgamecomputer = new computer();

            labcomputershowing.Text = thisgamecomputer.fist();


               // int num = fist.Next(0, 4);
        }

        private void NewMethod(string text)
        {
            player thisgamepalayer = new player();
            labplayershowing.Text = thisgamepalayer.fist(text);
        }

        private void btn_Scissors_Click(object sender, EventArgs e)
        {
            NewMethod(btn_Scissors.Text);
            NewMethod1();
            NewMethod2();
        }

        private void bth_Cloth_Click(object sender, EventArgs e)
        {
            NewMethod(bth_Cloth.Text);
            NewMethod1();
            NewMethod2();
        }
    }
}

结果:

  • 20
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISDF-工软未来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值