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();
        }
    }
}

结果:

相关推荐

  1. c++石头剪刀游戏

    2024-03-14 14:42:02       19 阅读
  2. Python列表实现石头剪刀游戏

    2024-03-14 14:42:02       18 阅读
  3. Python石头剪刀游戏

    2024-03-14 14:42:02       18 阅读
  4. HTML中js简单实现石头剪刀游戏

    2024-03-14 14:42:02       16 阅读
  5. oj 1.8编程基础之多维数组 16:矩阵剪刀石头

    2024-03-14 14:42:02       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 14:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 14:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 14:42:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 14:42:02       20 阅读

热门阅读

  1. Vue中的知识点

    2024-03-14 14:42:02       24 阅读
  2. CXL (Compute Express Link) Technology——论文阅读

    2024-03-14 14:42:02       37 阅读
  3. c++面经

    2024-03-14 14:42:02       19 阅读
  4. 人类的谋算与量子计算

    2024-03-14 14:42:02       15 阅读
  5. 用C语言链表实现图书管理

    2024-03-14 14:42:02       18 阅读
  6. 算法刷题day30:递归

    2024-03-14 14:42:02       23 阅读
  7. Dijkstra&floyed

    2024-03-14 14:42:02       20 阅读
  8. 3. Linux标准I/O库

    2024-03-14 14:42:02       18 阅读
  9. Linux 学习笔记(15)

    2024-03-14 14:42:02       20 阅读
  10. vue常用6种数据加密方式的使用

    2024-03-14 14:42:02       17 阅读
  11. Python 正则表达式

    2024-03-14 14:42:02       21 阅读