ASP.NETWeb开发(C#版)-day1-C#基础+实操

.NET

.NET简介

dotnet5(.NET)合并了.NET Framework、.NET Core(可以跨平台)

实操:创建项目

开发环境

我用的Visual Studio2022 先创建新项目

创建新项目
注意第一步和第二步

再点击下一步后

注意框选的部分

注意打钩的地方
成功

说明(了解即可):
   在找到相应文件位置后,点击 “生成” 下方会出现 成功 之后 Debug 下会生成很多文件
   其中 xxx.exe 是生成的可执行文件,在.NET平台是一种通用文件

说明
生成
在VS下方输出的-成功

执行

执行
成功执行

C#基础语法

C#基础语法

数据类型

  • 简单数据类型(值类型)
  • 引用类型(值存储在堆里面,地址存储在栈里面)
    简单数据类型(值类型)

变量

变量
变量

实操001_变量

namespace Demo001_变量
{
    internal class Program
    {   //"///"是注释 解释说明的作用
        /// <summary>
        /// 主方法,程序的入口
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {

            //声明变量存储数据
            string message;

            //给变量赋值
            message = "欢迎来到C#的世界";

            //使用变量
            Console.WriteLine(message);
            
            //存储员工的信息:工号,姓名,性别,入职日期,基本工资,部门,岗位
			string emplyeeNo, name;
			bool gender;
			DateTime jobInDateTime;
			double salary;
			string departmentName;
			string job;
			
			Console.Write("请输入工号:");
			emplyeeNo = Console.ReadLine();
			Console.Write("请输入姓名:");
			name = Console.ReadLine();
			Console.Write("请输入性别:");
			gender = Convert.ToBoolean(Console.ReadLine());
			Console.Write("请输入入职日期(yyyy-mm-dd):");
			jobInDateTime = Convert.ToDateTime(Console.ReadLine());
			Console.Write("请输入基本工资:");
			salary = Convert.ToDouble(Console.ReadLine());
			Console.Write("请输入部门:");
			departmentName = Console.ReadLine();
			Console.Write("请输入岗位:");
			job = Console.ReadLine();
			
			//输出个人信息
			Console.WriteLine($"工号:{emplyeeNo}\n" +
			    $"姓名:{name}\n" +
			    $"性别:{gender}\n" +
			    $"入职日期:{jobInDateTime}\n" +
			    $"基本工资:{salary}\n" +
			    $"部门:{departmentName}\n" +
			    $"岗位:{job}");
        }
    }
}

实例1

如何在一个解决方案 中创建另一个项目

如图
后面的步骤前面有讲过。

完成图

注意:
	点击 配置启动项 , 勾选 当前项目

启动项设置
![启动项设置]](https://img-blog.csdnimg.cn/fd028c9cac10418a891dfc2ab073013b.png)

在这里插入图片描述
三元运算符
比较运算符

运算符
运算符优先级

实操002

namespace Demo002_算术运算符
{
    internal class Program
    {
        static void Main(string[] args)
        {
            DateTime dateOfBirth= Convert.ToDateTime("1995-10-2");
            int age = DateTime.Now.Year - dateOfBirth.Year;
            Console.WriteLine("年龄:"+age);
            //age++ 和 ++age 区别
            //先用再加  先加再用

            bool gender = true;
            gender = false;
            //string sex = gender == true ? "男" : "女";
            string sex = !gender ? "男" : "女";
            Console.WriteLine($"性别:{sex}");

            Console.WriteLine();
            Console.Write("请输入账号:");
            string loginId = Console.ReadLine();
            Console.Write("请输入密码:");
            string loginPassword = Console.ReadLine();
            string loginMsg = loginId == "admin" && loginPassword =="123456" ? "登录成功" : "用户名或密码错误,登录失败";
            Console.WriteLine(loginMsg);


            
        }
    }
}

成功
失败

结构

选择结构
选择结构
选择结构

实操003-if else

namespace Demo003_选择结构
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请问是否进行C#学习:(y/n):");
            string input = Console.ReadLine().ToLower();//输入转换为小写     .ToUpper()转化为大写
            if (input != "y" && input != "n") {
                Console.WriteLine("输入有误");
            }
            else
            {
                if (input == "y")
                {
                    Console.WriteLine("继续阅读");
                }
                else
                {
                    Console.WriteLine("停止阅读");
                }
            }
            
        }
    }
}

输入y
输入n
随便输入

实操004-多分支

namespace Demo004_多分支
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*******年终奖判定程序**********");
            Console.WriteLine("请输入基本工资:");
            double salary = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入考核等级(ABCD):");
            char level = Convert.ToChar(Console.ReadLine().ToUpper());
            double reward;//奖金
            if (level < 'A' || level > 'D')
                Console.WriteLine("等级输入有误");
            //多分支
            else
            {
                //if (level == 'A')
                //reward = salary * 6;
                //else if (level == 'B')
                //reward = salary * 3;
                //else if (level == 'C')
                //reward = salary * 2;
                //else
                //reward = salary;


                //只能写等值判断
                switch (level)
                {
                    case 'A':
                        reward = salary * 6;
                        break;
                    case 'B':
                        reward = salary * 3;
                        break;
                    case 'C':
                        reward = salary * 2;
                        break;
                    default:
                        reward = salary;
                        break;
                }
                Console.WriteLine($"年终奖是{reward}");
            }
        }
    }
}

A等级
B等级
C等级
D等级
输入有误

多行注释按钮

多行注释

循环结构
while循环
do while循环

在这里插入图片描述
在这里插入图片描述

实操:循环

namespace Demo005_循环结构
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //for
            int i;//定义在外面比较好
            for(i = 0; i < 3; i++) {
                Console.WriteLine("重要的事情说三遍");
            }
            
            //登录系统,输入用户名和密码,三次有效
            string userName, password;

            for(i = 0;i < 3; i++)
            {
                Console.WriteLine($"第{i + 1}次登录开始......");
                Console.Write("请输入用户名:");
                userName= Console.ReadLine();
                Console.Write("请输入密码:");
                password = Console.ReadLine();

                if(userName == "admin" && password == "admin") {
                    Console.WriteLine("登录成功");
                    break;//强制退出循环
                }
                else if (i < 2) {
                    Console.WriteLine("用户名或密码错误,登录失败");
                }
                else
                {
                    Console.WriteLine("三次机会已用完,账号已锁定");
                }

            }
        }
    }
}

成功
三次全错
有错

面向对象基础

面向对象基础
类

如何在同一个项目下创建新的.cs文件

类

实操-类的定义与访问

Employee.cs

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

namespace Demo006_类的定义和访问
{
    /**
     * 类名:Employee
     * 功能:模拟所有的职员对象
     * 
     */
    public class Employee
    {//internal 去掉也不会报错 internal只能在这里访问到 public是都可以访问到

        //成员变量(字段):特征
        //string employeeNo;//这是私有的private
        public string employeeNo;
        public string name;
        public bool gender;
        public double salary;

        //构造方法:在实例化对象时调用
        //构造方法名称必须与类名一致
        public Employee()
        {//默认存在,但是调用了带参构造,在没有定义无参构造的时候,调用无参会报错,建议带上无参构造
            //Console.WriteLine("正在实例化员工对象...");
        }

        /// <summary>
        /// 带参数构造方法
        /// </summary>
        /// <param name="employeeNo">员工号</param>
        /// <param name="name">姓名</param>
        /// <param name="gender">性别</param>
        /// <param name="salary">工资</param>
        public Employee(string employeeNo, string name, bool gender, double salary)
        {
            //this关键字:正在实例化的对象
            this.employeeNo = employeeNo;
            this.name = name;
            this.gender = gender;
            this.salary = salary;
        }

        //方法:对象的行为能力
        public void ShowEmployeeMsg()
        {
            Console.WriteLine($"{this.employeeNo}\t{this.name}\t{(this.gender == true ? "男" : "女")}\t{this.salary}");
        }
    }
}

Program.cs

namespace Demo006_类的定义和访问
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //实例化对象
            Employee emp01 = new Employee();

            //访问变量:对象名.变量名
            emp01.employeeNo = "1234";
            emp01.name = "张三";
            emp01.gender = true;
            emp01.salary = 6589;

            Employee emp02 = new Employee();
            emp01.employeeNo = "1235";
            emp01.name = "王小二";
            emp01.gender = false;
            emp01.salary = 7800;

            Employee emp03 = new Employee("1236", "rose", false, 6500);

            //Console.WriteLine($"{emp01.employeeNo}\t{emp01.name}\t{(emp01.gender == true? "男":"女")}\t{emp01.salary}");
            //Console.WriteLine($"{emp02.employeeNo}\t{emp02.name}\t{(emp02.gender == true ? "男" : "女")}\t{emp02.salary}");
            //Console.WriteLine($"{emp03.employeeNo}\t{emp03.name}\t{(emp03.gender == true ? "男" : "女")}\t{emp03.salary}");

            //调用方法:对象名.方法名
            emp01.ShowEmployeeMsg();
            emp02.ShowEmployeeMsg();
            emp03.ShowEmployeeMsg();
        }
    }
}

实操-练习

使用OOP的思想模拟个人手机的信息,包含手机品牌,型号,价格和颜色
开机和关机的功能

Phone.cs

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

namespace Demo007_练习
{
    /**
     * 类名:Phone
     * 功能:模拟手机对象
     * 
     */
    public class Phone
    {
        public string brand;//品牌
        public string type;//型号
        public double price;//价格
        public string color;//颜色

        public Phone()
        {
            Console.WriteLine("正在实例化手机对象...");
        }

        public Phone(string brand, string type, double price, string color)
        {
            this.brand = brand;
            this.type = type;
            this.price = price;
            this.color = color;
        }

        public void OpenPhone()
        {
            Console.WriteLine($"{this.brand}品牌{this.type}型号{this.price}{this.color}的手机正在开机......");
        }

        public void ClosePhone()
        {
            Console.WriteLine($"{this.brand},{this.type},{this.price},{this.color}" + "关机了");
        }
    }
    
    
}

实现结果

实操-方法

Employee.cs

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

namespace Demo006_类的定义和访问
{
    /**
     * 类名:Employee
     * 功能:模拟所有的职员对象
     * 
     */
    public class Employee //internal 去掉也不会报错 internal只能在这里访问到 public是都可以访问到
    {
        //成员变量(字段):特征
        //string employeeNo;//这是私有的private
        public string employeeNo;
        public string name;
        public bool gender;
        public double salary;

        //构造方法:在实例化对象时调用
        //构造方法名称必须与类名一致
        public Employee() {//默认存在,但是调用了带参构造,在没有定义无参构造的时候,调用无参会报错,建议带上无参构造
            Console.WriteLine("正在实例化员工对象...");
        }

        public Employee(string employeeNo, string name, bool gender, double salary)
        {
            //this关键字:正在实例化的对象
            this.employeeNo = employeeNo;
            this.name = name;
            this.gender = gender;
            this.salary = salary;
        }

        //方法:对象的行为能力
        public void ShowEmployeeMsg()
        {
            Console.WriteLine($"{this.employeeNo}\t{this.name}\t{(this.gender == true ? "男" : "女")}\t{this.salary}");
        }

        //请假
        public void SendMsg(string type,DateTime beginDate,int days,string reason)
        {
            Console.WriteLine($"{this.employeeNo}的员工申请{type}");
            Console.WriteLine($"开始日期:{beginDate}\n" +
                $"请假天数:{days}\n" +
                $"结束日期:{beginDate.AddDays(days)}\n" +
                $"请假事由:{reason}\n");

        }

        //年终奖
        public double GetReward(string level)
        {
            double reward;
            switch (level)
            {
                case "A":
                    reward = this.salary * 6;
                    break;
                case "B":
                    reward = this.salary * 3;
                    break;
                case "C":
                    reward = this.salary * 2;
                    break;
                default:
                    reward = this.salary;
                    break;
            }
            return reward;//返回语句
        }
    }
}

Program.cs

using Demo006_类的定义和访问;

namespace Demo008_方法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Employee employee1 = new Employee("1234","张三",true,5000);
            employee1.SendMsg("事假", Convert.ToDateTime("2023-11-10 09:00:00"), 2, "家里有事");

            Employee employee2 = new Employee("1235", "李四", true, 6000);
            employee2.SendMsg("婚假", DateTime.Now, 10, "回家结婚");

            Console.Write("请输入考核等级:");
            string inputLevel = Console.ReadLine();
            double money = employee1.GetReward(inputLevel);
            Console.WriteLine($"年终奖金是:{money}");

        }
    }
}

结果

实操:计算器

Calculator.cs

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

namespace Demo009_Calculator
{
    public class Calculator
    {
       

        public int GetResult(int a, int b, string type)
        {
            int c = 0;
            if (type == "+")
            {
                c = a + b;
            }
            else if (type == "-")
            {
                c = a - b;
            }
            else if (type == "*")
            {
                c = a * b;
            }
            else if (type == "/")
            {
                if (b == 0)
                {
                    Console.WriteLine("除数为0无法计算");
                }
                else
                {
                    c = a / b;
                }
            }
            return c;

        }
    }

    
}

Program.cs

namespace Demo009_Calculator
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //使用OOP思想实现两个数的加减乘除运算。
            Calculator calculator = new Calculator();
            Console.Write("请输入第一个数:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入第二个数:");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入运算符(+/ - / * / /):");
            string type = Console.ReadLine();

            //计算
            int c = calculator.GetResult(a, b, type);
            Console.WriteLine($"{a}{type}{b}={c}");
        }
    }
}

结果

综合实例

*以OOP的思想实现猜拳游戏:
*计算机和用户实现猜拳,可以出剪刀、石头和布。
*剪刀用0表示,石头用1表示,布用2表示。
*程序启动,系统默认可以玩10局,用户玩完一局之后可以按任意键继续,按q退出,退出后需显示实际玩了几局,用户赢了几局,电脑赢了几局,平了几局,如果用户赢的局数大于电脑赢的局数,显示用户大获全胜;如果电脑赢的局数大于用户赢的局数,显示用户败给了电脑;如果赢的局数相同,显示打成了平手。

*每一局游戏的游戏规则:
*先用户出拳,输入0 - 2为后显示用户出的拳是什么,如果用户出的不是0 - 2,提示用户输入错误,重新输入,直到用户输入正确为止,
*再由电脑随机出拳,电脑产生0 - 2之间的随机数,也要显示电脑出的拳是什么,然后判断电脑和用户的输赢关系,并给出适当的提示,比如本局是用户赢了,还是电脑赢了,还是平局

Player.cs

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

namespace Demo010_综合案例
{ 
    /// <summary>
    /// 玩家类
    /// </summary>
    public class Player
    {
        public string name;//玩家的昵称

        /// <summary>
        /// 玩家出拳
        /// </summary>
        /// <returns></returns>
        public int Throw()
        {
            while (true)
            {
                try
                {
                    Console.WriteLine($"请{this.name}出拳");
                    int point = Convert.ToInt32(Console.ReadLine());

                    if (point >= 0 && point < 3)
                    {
                        return point;
                    }
                    else
                        Console.WriteLine("输入有误,请输入0-2");
                }catch (Exception ex)
                {
                    Console.WriteLine ("输入有误,请输入数字");
                }
            }
        }
    }
}

Computer.cs

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

namespace Demo010_综合案例
{
    public class Computer
    {

       public int CreateRandomNum()
        {
            Random r = new Random();
            return r.Next(3);
        }

    }
}

GuessGame.cs

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

namespace Demo010_综合案例
{
    /// <summary>
    /// 猜拳游戏类
    /// </summary>
    public class GuessGame
    {
        Player player = new Player();
        Computer computer = new Computer();

        /// <summary>
        /// 输入玩家的昵称
        /// </summary>
        public void InputePlayName()
        {
            Console.Write("请输入昵称:");
            player.name = Console.ReadLine();
        }

        /// <summary>
        /// 欢迎界面/打印界面
        /// </summary>
        public void ShowMsg() {
            Console.WriteLine("*****************************");
            Console.WriteLine("******欢迎来到猜拳游戏*******");
            Console.WriteLine("*****************************");
        }

        /// <summary>
        /// 程序启动
        /// </summary>
        public void Start()
        {
            this.ShowMsg();
            InputePlayName();

            int p1 = player.Throw();
            int p2 = computer.CreateRandomNum();

            string quan1 = ConvertInToString(p1);
            string quan2 = ConvertInToString(p2);
            Console.WriteLine($"{player.name}出的拳是{quan1}");
            Console.WriteLine($"电脑出的拳是{quan2}");
            Judge(p1, p2);

            Console.WriteLine("是否继续下一局,按任意键继续,按q退出");
            string input = Console.ReadLine().ToLower();
            if(input == "q")
            {
                Console.WriteLine("游戏正在退出...");
                Console.ReadKey();//需要按一个键

            }
            Console.ReadKey();
            Console.Clear();

        }

        /// <summary>
        /// 数字的点数转换为字符串的拳
        /// </summary>
        /// <param name="point">点数</param>
        /// <returns>拳</returns>
        public string ConvertInToString(int point)
        {
            if(point == 0) {
                return "剪刀";
            }
            if (point == 1)
            {
                return "石头";
            }
            return "布";
            
        }
        /// <summary>
        /// 判断输赢
        /// </summary>
        /// <param name="playerPoint">玩家的点数</param>
        /// <param name="computerPoint">电脑的点数</param>
        public void Judge(int playerPoint, int computerPoint)
        {
            //0(剪刀) 1(石头) 2(布)

            //用户赢:0(2)=-2,1(0)=1,2(1)=1
            int diff = playerPoint - computerPoint;
            if (diff == 0) {

                Console.WriteLine("平局");
            }else if (diff ==-2 || diff ==1) {
                Console.WriteLine($"用户{player.name}赢了一局");
            }
            else
            {
                Console.WriteLine("电脑赢了一局");
            }
        }
    }
}

Program.cs

namespace Demo010_综合案例
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //以OOP的思想实现猜拳游戏:
            //计算机和用户实现猜拳,可以出剪刀、石头和布。
            //剪刀用0表示,石头用1表示,布用2表示。
            //程序启动,系统默认可以玩10局,用户玩完一局之后可以按任意键继续,按q退出,退出后需显示实际玩了几局,用户赢了几局,电脑赢了几局,平了几局,如果用户赢的局数大于电脑赢的局数,显示用户大获全胜;如果电脑赢的局数大于用户赢的局数,显示用户败给了电脑;如果赢的局数相同,显示打成了平手。

            //每一局游戏的游戏规则:
            //先用户出拳,输入0 - 2为后显示用户出的拳是什么,如果用户出的不是0 - 2,提示用户输入错误,重新输入,直到用户输入正确为止,
            //再由电脑随机出拳,电脑产生0 - 2之间的随机数,也要显示电脑出的拳是什么,然后判断电脑和用户的输赢关系,并给出适当的提示,比如本局是用户赢了,还是电脑赢了,还是平局
            
            GuessGame guessGame = new GuessGame();
            guessGame.Start();
            Player p = new Player(); 
            p.name = "张三";
            
        }
    }
}

结果

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值