dotnet5(.NET)合并了.NET Framework、.NET Core(可以跨平台)
我用的Visual Studio2022 先创建新项目
再点击下一步后
说明(了解即可):
在找到相应文件位置后,点击 “生成” 下方会出现 成功 之后 Debug 下会生成很多文件
其中 xxx.exe 是生成的可执行文件,在.NET平台是一种通用文件
namespace Demo001_变量
{
internal class Program
{ //"///"是注释 解释说明的作用
///
/// 主方法,程序的入口
///
///
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}");
}
}
}
后面的步骤前面有讲过。
注意:
点击 配置启动项 , 勾选 当前项目
![启动项设置]](https://img-blog.csdnimg.cn/fd028c9cac10418a891dfc2ab073013b.png)
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);
}
}
}
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("停止阅读");
}
}
}
}
}
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}");
}
}
}
}
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("三次机会已用完,账号已锁定");
}
}
}
}
}
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("正在实例化员工对象...");
}
///
/// 带参数构造方法
///
/// 员工号
/// 姓名
/// 性别
/// 工资
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_综合案例
{
///
/// 玩家类
///
public class Player
{
public string name;//玩家的昵称
///
/// 玩家出拳
///
///
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_综合案例
{
///
/// 猜拳游戏类
///
public class GuessGame
{
Player player = new Player();
Computer computer = new Computer();
///
/// 输入玩家的昵称
///
public void InputePlayName()
{
Console.Write("请输入昵称:");
player.name = Console.ReadLine();
}
///
/// 欢迎界面/打印界面
///
public void ShowMsg() {
Console.WriteLine("*****************************");
Console.WriteLine("******欢迎来到猜拳游戏*******");
Console.WriteLine("*****************************");
}
///
/// 程序启动
///
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();
}
///
/// 数字的点数转换为字符串的拳
///
/// 点数
/// 拳
public string ConvertInToString(int point)
{
if(point == 0) {
return "剪刀";
}
if (point == 1)
{
return "石头";
}
return "布";
}
///
/// 判断输赢
///
/// 玩家的点数
/// 电脑的点数
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 = "张三";
}
}
}