第十一讲 分支结构
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Product
{
public string name; //名称
public decimal price; //价格
public int amout; //数量
public bool discontinued; //停售
}
public enum Department
{
行政部,
财务部,
人事部,
市场部
}
class Employee
{
public string name;
public decimal salary;
public string gender;
public Department department;
}
class Program
{
static void Main(string[] args)
{
//if语句
Product p = new Product();
p.name = "iPhone12";
p.price = 9800.00m;
p.amout = 30;
p.discontinued = false;
//1.if
if(p.amout<0) //条件表达式
{
p.discontinued = true;//子语句
}
//2.if .... else....
string result = "";
if(p.amout<=0)
{
result = "已售空!";
}
else
{
result = "剩余" + p.amout + "件";
}
Console.WriteLine(result);
Console.ReadLine();
//3.if .... else if .... else....
//低于3000不打折,3000-5000九折,5000-8000八折,8000以上七折
decimal newPrice = 0.0m;
if (p.price <= 3000)
{
newPrice = p.price;
}
else if (p.price <= 5000)
{
newPrice = p.price * 0.9m;
}
else if (p.price <= 80000)
{
newPrice = p.price * 0.8m;
}
else
newPrice = p.price * 0.7m;
Console.WriteLine(newPrice);
Console.ReadLine();
//switch语句(多重分支)
Employee emp = new Employee();
emp.name = "TOM";
emp.salary = 3650.00m;
emp.gender = "男";
emp.department = Department.市场部;
switch(emp.gender)
{
case "男":
emp.salary += 500;
break;
case "女":
emp.salary += 1000;
break;
}
//根据所在部门调整薪资
switch (emp.department) //括号里一定要是常量
{
case Department.人事部:
emp.salary += 500;
break; //每一种情况后,break 一定要有。
case Department.市场部:
emp.salary += 600;
break;
case Department.行政部:
emp.salary += 800;
break;
case Department.财务部:
emp.salary += 1000;
break;
default:
emp.salary += 100;
break;
}
Console.WriteLine("{0}的工资是:{1},他的部门是:{2}", emp.name, emp.salary,emp.department);
Console.ReadLine();
//练习
//1.计算个人所得税,假设个人所得税为:税率 *(工资 - 1600)。请编写程序计算应缴的所得税,其中税率定义为:
//(1)当工资不超过1600时,税率为0;
//(2)当工资在区间(1600,2500]时,税率为5 %;
//(3)当工资在区间(2500,3500]时,税率为10 %;
//(4)当工资在区间(3500,4500]时,税率为15 %;
//(5)当工资超过4500时,税率为20 %。
//2.企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10 %;
//利润高于10万元,低于20万元时,低于10万元的部分按10 % 提成,
//高于10万元的部分,可可提成7.5 %;
//20万到40万之间时,高于20万元的部分,可提成5 %;
//40万到60万之间时高于40万元的部分,可提成3 %;
//60万到100万之间时,高于60万元的部分,可提成1.5 %,
//高于100万元时,超过100万元的部分按1 % 提成,
//从键盘输入当月利润I,求应发放奖金总数?
}
}
}