输入密码账号进行登录,同时判断用户登录身份权限,
关键技术
if判断,逻辑运算符
实现
class Base002
{
///
/// 输入账号 和 密码 进行权限 登录
///
/// 账号
/// 密码
/// 权限
public int Login(string id, string password)
{
// 内部设置了两个账号
string myOneId = "001", myOnePassWord = "myone";
string myId = "101", myPassWord = "myID";
if (id.Equals(myOneId) && password.Equals(myOnePassWord))
{
Console.WriteLine("欢迎VIP客户{0}登录成功!", id);
return 1;
}
if (id.Equals(myId) && password.Equals(myPassWord))
{
Console.WriteLine("欢迎客户{0}登录成功!", id);
return 2;
}
Console.WriteLine("用户不存在,或密码错误!");
return 0;
}
}
mian方法
static void Main(string[] args)
{
Base002 b2 = new Base002();
Console.Write("请输入账号:");
string id = Console.ReadLine();
Console.Write("请输入密码:");
string password = Console.ReadLine();
int grade = b2.Login(id, password);
Console.WriteLine("权限等级:" + grade);
}
总结
string.Equals(“值”) - 判断两个字符串的值是否相等
后续优化:账号和密码可动态获取
- 版本优化
一组数:1,1,2,3,5,8,13,21……
第30个数是多少 (832040)
关键技术
规则:1+1=2, 1+2=3, 2+3=5,3+5=8 ……
实现
////// 递归 1,1,2,3,5 …… /// /// 第几位的值 ///值 public int Recursion(int index) { if (index <= 2) { return 1; } return Recursion(index - 1) + Recursion(index - 2); }
总结
类似于数学中函数思维
f(index) = f(index - 1) + f(index - 2)
然后在判断退出条件
随机生成一个三位数,猜测大小
大返回偏大,小返回偏小,直至猜测正确
关键技术
随机数,switch
实现
class Base002
{
Random random = new Random();
int randKey;
public Base002()
{
randKey = random.Next(100, 999);//随机数
Console.WriteLine("要猜测的数值 = " + randKey);
}
///
/// 比较数的大小
///
/// 数字
/// 1-偏大;0-相等,-1-偏小
public int GuessNumber(int number)
{
int cha = number - randKey;
if (cha > 0) { return 1; } //偏大
else if (cha == 0) { return 0; } //相等
else { return -1; }//偏小
}
}
main方法
static void Main(string[] args)
{
Base002 b2 = new Base002();
do
{
Console.Write("请输入要猜测的三位数:");
int num = b2.GuessNumber(int.Parse(Console.ReadLine()));
switch (num)
{
case 1: Console.WriteLine("偏大"); break;
case 0: Console.WriteLine("恭喜,猜对了!"); return;
case -1: Console.WriteLine("偏小"); break;
default: Console.WriteLine("输入不合法"); break;
}
} while (true);
}
总结
随机数:Random
- 时间:2021.10.23
- 完成:2021.10.23



