模拟用户登录
使用if语句实现用户登录的基本功能,用户名密码随意定义。
namespace wxn
{
internal class denglu
{
static void Main(string[] args)
{
//定义字符串变量
String uu, name, mm;
Console.WriteLine("\t\t1. root用户 \n \t\t2. 普通用户");
Console.WriteLine("\t\t 选择要登录的账户(1,2):");
uu = Console.ReadLine();
Console.Write("请输入用户名:");
name = Console.ReadLine();
Console.Write("请输入密码:");
mm = Console.ReadLine();
//使用if语句进行判断
if (uu == "1")
{
if (name == "root" && mm == "root888")
Console.WriteLine("尊敬的" + name + "用户,欢迎你登录!");
else
Console.WriteLine("你的用户名或密码错误!");
}
if (uu == "2")
{
if (name == "wwjj" && mm == "www777")
Console.WriteLine("尊敬的" + name + "用户,欢迎你登录!");
else
Console.WriteLine("你的用户名或密码错误!");
}
if(uu!= "1" && uu!= "2")
Console.WriteLine("你输入的用户类型有误!");
}
}
}
运行结果如图所示
普通用户运行结果
判断身高体重比
根据性别,身高及体重,判断身高体重比是否正确。
namespace wxn
{
internal class shengao
{
static void Main(string[] args)
{
//定义字符串变量和整型变量
string sex;
int height, weight;
Console.WriteLine("\t\t 请输入性别(1:男 2:女):");
sex = Console.ReadLine();
Console.WriteLine("请输入你的身高(cm):");
height = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你的体重(kg):");
weight = Convert.ToInt32(Console.ReadLine());
//使用if语句判断
if (sex == "1")
{
if ( height - 105 - weight<=2)
Console.WriteLine("你的身高体重比很正常!");
else
Console.WriteLine("你的身高体重比不正常!");
}
if (sex == "2")
{
if (height - 110 - weight <= 2)
Console.WriteLine("你的身高体重比很正常!");
else
Console.WriteLine("你的身高体重比不正常!");
}
if (sex != "1" && sex != "2")
Console.WriteLine("你输入的用户类型有误!");
}
}
}
运行结果如图所示
数字排序
输入三个数按照从大到小进行排序。
namespace wxn
{
internal class daxiao
{
static void Main(string[] args)
{
int x, y,z,g;
Console.WriteLine("请输入第一个数:");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个数:");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第三个数:");
z = Convert.ToInt32(Console.ReadLine());
if (x > y)
{
g = x;
x = y;
y = g;
}
if (x > z)
{
g = x;
x = z;
z = y;
y = g;
}
if (y > z)
{
g = y;
y = z;
z = g;
}
Console.WriteLine("三个数从大到小排序为:{0}>{1}>{2}",z,y,x);
}
}
}
运行结果如图所示
超市收银功能
所有超市会员享受购物9折优惠
对于非超市会员的顾客购物超过100元的也享受购物9折优惠
本身是超市会员并且购物超过100元可以享受购物8折优惠
假如顾客从超市购买三件商品,请计算该顾客可以达到的折扣额(如果有),以及打折后应付的款额。
namespace wxn
{
internal class huiyuan
{
static void Main(string[] args)
{
//定义字符串变量和浮点型变量
string hy;
double x, y, z,zj;
Console.WriteLine("是否为会员(是or不是):");
hy = Convert.ToString(Console.ReadLine());
Console.WriteLine("请输入第一个商品价格:");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个商品价格:");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第三个商品价格:");
z = Convert.ToInt32(Console.ReadLine());
//zj为三个商品的总价
zj = x + y + z;
//进行判断
if(hy.Equals("是") && zj > 100)
{
zj = 0.8 * zj;
Console.WriteLine("打八折,应付款:"+ zj +"元");
}
else
if((hy.Equals("是") && zj <= 100)||(hy.Equals("不是") && zj > 100))
{
zj = 0.9 * zj;
Console.WriteLine("打九折,应付款:" + zj + "元");
}
else
{
Console.WriteLine("你应付款:"+ zj + "元");
}
}
}
}
运行结果如图所示
工作日or休息日
根据用户输入今天是星期几,根据用户输入判断是工作日还是周末,并显示合适的问候语。
namespace wxn
{
internal class xingqi
{
static void Main(string[] args)
{
int y;
Console.WriteLine("输入今天是星期几(1-7):");
y = Convert.ToInt32(Console.ReadLine());
if(y>=1 && y <= 7)
{
if (y >= 1 && y <= 5)
{
Console.WriteLine("今天是工作日!!!");
}
else
{
Console.WriteLine("今天是休息日!!!");
}
}
}
}
}
运行结果如图所示
超市收银折扣
使用if语句进行判断,只要顾客购买的商品数量大于等10件,并且购物的总额度大于等于10000,商店就会给顾客10%的折扣。接受商品总数量和总额度,要求计算出折扣额和折扣后应付金额。
namespace wxn
{
internal class goumai
{
static void Main(string[] args)
{
//定义整型变量和浮点型变量
int s;
double z,x;
Console.WriteLine("输入购买的商品数量:");
s = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入购买的商品的总额度:");
z = Convert.ToDouble(Console.ReadLine());
//x是是享受的折扣金额
x = z * 0.1;
if (s >= 10 && z >= 10000)
{
z = z * 0.9;
Console.WriteLine("你的额度大于10000,享受"+ x +"的折扣,应付" + z + "元!!!");
}
//以下是不符合折扣规则的两种提示结果
else
if (s < 10 && z < 10000 )
{
Console.WriteLine("你的商品数量小于10件,且额度小于10000,不符合折扣要求!!!");
}
else
if(s > 10 && z < 10000)
{
Console.WriteLine("你的商品数量大于10件,但额度小于10000,不符合折扣要求!!!");
}
}
}
}
运行结果如图所示
第二种运行结果
第三种运行结果
和差运算
接受用户输入的一个字符,例如用“+”表示加法,“-”表示减法。接受两个数num1和num2,如果输入的数据是“+”则将两个数相加并显示相应结果。如果输入的数据是“-”则将两个数相减并显示相应结果。
namespace wxn
{
internal class jisuan
{
static void Main(string[] args)
{
//定义字符串变量和整型变量
String s;
int z, x;
//使用1,2代表计算符号+,-
Console.Write("输入计算的字符(1.+/2.-):");
s = Console.ReadLine();
if (s == "1" || s == "2")
{
Console.Write("请输入第一个数字:");
z = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入第二个数字:");
x = Convert.ToInt32(Console.ReadLine());
//进行判断
if (s == "1")
{
Console.Write("这两个数相加得:{0}+{1}={2}", z, x, z + x);
}
else
if (s == "2")
{
Console.Write("这两个数相减得:{0}-{1}={2}", z, x, z - x);
}
}
else
{
Console.WriteLine("输入的字符不符合要求!!!");
}
}
}
}
加法运行结果
减法运行结果
结语: 以上均为单分支和二分支选择语句的应用,复杂的点是需要进行多次判断时对于二分支语句的多次运用。