1. 三目运算符、if语句、switch语句(分支结构)
using System;
namespace test_1
{
class Program
{
static void Main(string[] args)
{
//分支结构
#region 三目运算符
/*
//运算符的分支结构,三目(元)运算符 "?:"
//缺点:冒号前后只能是具体的结果,不能是语句
//判断数学成绩是否合格
Console.WriteLine("请输入一个整数:");
int mathScore = int.Parse(Console.ReadLine());
string result = mathScore >= 60 ? "及格":"不及格";
Console.WriteLine("Result : " + result);
//【练习】
//1.判断伤害数值
//攻击力大于100,伤害-->10;攻击力小于等于100,伤害-->5
Console.WriteLine("请输入一个整数:");
int attack = int.Parse(Console.ReadLine());
int hurt = attack > 100 ? 10 : 5;
Console.WriteLine("hurt : " + hurt);
//2.用三目运算符求绝对值
Console.WriteLine("请输入一个整数:");
int number = int.Parse(Console.ReadLine());
int abs = number >= 0 ? number : -number;
Console.WriteLine(abs);
*/
#endregion
#region if语句[第一种形态]
/*
//if语句
//if (条件表达式)
//{
// 执行语句;
//}
int num = 110;
if (num >100)
{
Console.WriteLine("数字大于100");
}
//【练习】
Console.WriteLine("请输入一个整数:");
int number = int.Parse(Console.ReadLine());
if (number > 18)
{
Console.WriteLine("结果:" + number * 2);
}
*/
#endregion
#region if语句[第二种形态]
/*
//if语句
//if (条件表达式)
//{
// 执行语句;
//}
//else{
// 执行语句;
//}
//技能可以使用的剩余时间
float remainingTime = 10;
if (remainingTime == 0)
{
Console.WriteLine("可以使用技能");
}
else
{
Console.WriteLine("不能使用技能");
}
//【练习】
//1.输入两个整数,求两者最大值
Console.WriteLine("请输入两个整数:");
int number01 = int.Parse(Console.ReadLine());
int number02 = int.Parse(Console.ReadLine());
if (number01 > number02)
{
Console.WriteLine("最大值是:" + number01);
}
else
{
Console.WriteLine("最大值是:" + number02);
}
//2.输入一个年份判断是否是闰年
Console.WriteLine("请输入一个年份:");
int year = int.Parse(Console.ReadLine());
if (year % 400 == 0||(year % 100 != 0 && year % 4 == 0))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("不是闰年");
}
*/
#endregion
#region if语句[第三种形态]-级联式
/*
//if语句
//if (条件表达式)
//{
// 执行语句;
//}
//else if{
// 执行语句;
//}
//else{
// 执行语句;
//}
float score = 77;
if (score >= 90)
{
Console.WriteLine("优秀");
}
else if (score >= 70 )
{
Console.WriteLine("良好");
}
else if (score >= 60 )
{
Console.WriteLine("及格");
}
else if(score >= 0)
{
Console.WriteLine("不及格");
}
//【练习】
//判断输入的数据类型,根据ASCII码来判断
//是数字 48 - 57
//是大写字母 65 - 90
//是小写字母 97 - 122
//其他
Console.WriteLine("请输入一个字符");
int achar = Console.ReadLine();
if (achar >= 48 && achar <= 57)
{
Console.WriteLine("这是一个数字...");
}
else if (achar >= 65 && achar <= 90)
{
Console.WriteLine("这是一个大写字母...");
}
else if (achar >= 97 && achar <= 122)
{
Console.WriteLine("这是一个小写字母...");
}
else
{
Console.WriteLine("这是其他字符...");
}
*/
#endregion
#region switch语句
/*
//switch (表达式) 括号里必须是一个变量或常量,值类型要和判断数据类型要一致
//{
// case 值1:
// {
// 语句1;break;
// }
// case 值2:
// {
// 语句2;break;
// }
// ...
// case 值n:
// {
// 语句n;break;
// }
// default:
// {
// 语句n + 1;
// break;
// }
//}
char autumn = '冬';
switch (autumn)
{
case '春' :
Console.WriteLine("春困...");
break;//跳出当前语句块【switch的大括号】
case '秋' :
Console.WriteLine("秋乏...");
break;
case '夏' :
Console.WriteLine("夏打盹...");
break;
case '冬' :
Console.WriteLine("睡不醒...");
break;
default:
Console.WriteLine("输入错误...");
break;
}
//【练习】
//判断月份
int month = int.Parse(Console.ReadLine());
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("31天");
break;
case 4:
case 6:
case 9:
case 11:
Console.WriteLine("30天");
break;
case 2:
Console.WriteLine("28天");
break;
default:
Console.WriteLine("输入错误");
break;
}
*/
#endregion
//阿里面试题:求一个浮点数是否等于0,浮点数是有误差的
Console.WriteLine("请输入要判断的数");
float num = float.Parse(Console.ReadLine());
//求误差
float s = num - 0;
//求绝对值
s = s > 0 ? s : -s;
if (s < 0.001f)
{
Console.WriteLine("等于0");
}
else
{
Console.WriteLine("不等于0");
}
//if 和 switch 的区别
//1.if可以去判断很多关系运算符,switch只能去判断 ==
//2.switch可以同时判断多种情况,且便捷,可读性高,而if判断多个的时候,可读性不高,代码冗长
}
}
}
2.作业
using System;
namespace Day04_3
{
class Program
{
static void Main(string[] args)
{
//Day04作业
/* #region 第一题------计算器
Console.WriteLine("请输入第一个数字:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入运算符:");
char str = Char.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数字");
int num2 = int.Parse(Console.ReadLine());
//if语句实现
if (str == '+')
{
Console.WriteLine("计算结果为:" + (num1 + num2));
}
else if (str == '-')
{
Console.WriteLine("计算结果为:" + (num1 - num2));
}
else if(str == '*')
{
Console.WriteLine("计算结果为:" + (num1 * num2));
}
else if(str == '/')
{
if (num2 == 0)
{
Console.WriteLine("除数不能为0"); //--------------------------除数不能为0
}
else
{
Console.WriteLine("计算结果为:" + (num1 / num2));
}
}
else if (str == '%')
{
Console.WriteLine("计算结果为:" + (num1 % num2));
}
else
{
Console.WriteLine("您输入的运算符不合法");
}
//switch语句实现
//switch (str)
//{
// case '+':
// Console.WriteLine("计算结果为:" + (num1 + num2));
// break;
// case '-':
// Console.WriteLine("计算结果为:" + (num1 - num2));
// break;
// case '*':
// Console.WriteLine("计算结果为:" + (num1 * num2));
// break;
// case '/':
// if (num2 == 0) //----------------------------switch里可以嵌套if语句
// {
// Console.WriteLine("除数不能为0");
// }
// else
// {
// Console.WriteLine("计算结果为:" + (num1 / num2));
// }
// break;
// case '%' :
// Console.WriteLine("计算结果为:" + (num1 % num2));
// break;
// default:
// Console.WriteLine("您输入的运算符不合法");
// break;
//}
#endregion
#region 第二题------------日历
Console.WriteLine("请输入月:");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("请输入:日");
int day = int.Parse(Console.ReadLine());
//用If语句实现
float a = day / 100;
float s = month + a;
if (s >= 3.21 && s <= 4.19)
{
Console.WriteLine("您是白羊座");
}
else if (s >= 4.20 && s <= 5.20)
{
Console.WriteLine("您是金牛座");
}
else if (s >= 5.21 && s <= 6.21)
{
Console.WriteLine("你是双子座");
}
else if (s >= 6.22 && s <= 7.22)
{
Console.WriteLine("您是巨蟹座");
}
else if (s >= 7.23 && s <= 8.22)
{
Console.WriteLine("您是狮子座");
}
else if (s >= 8.23 && s <= 9.22)
{
Console.WriteLine("您是处女座");
}
else if (s >= 9.23 && s <= 10.23)
{
Console.WriteLine("您是天秤座");
}
else if (s >= 10.24 && s <= 11.22)
{
Console.WriteLine("您是天蝎座");
}
else if (s >= 11.23 && s <= 12.21)
{
Console.WriteLine("您是射手座");
}
else if (s >= 12.22 && s <= 1.19)
{
Console.WriteLine("您是摩羯座");
}
else if (s >= 1.20 && s <= 2.18)
{
Console.WriteLine("您是水瓶座");
}
else if (s >= 2.19 && s <= 3.20)
{
Console.WriteLine("您是双鱼座");
}
//用switch语句实现
//switch (month)
//{
// case 1:
// if (day>= 1 && day <= 19)
// {
// Console.WriteLine("您是摩羯座");
// }
// else if (day >= 20 && day <= 31)
// {
// Console.WriteLine("您是水瓶座");
// }
// break;
// ....
// default:
// Console.WriteLine("您输入日期有误");
//}
#endregion
#region 第三题
//打印出1-100之间所有的偶数
int num = 2;
while (num <100)
{
Console.WriteLine(num);
num += 2;
}
#endregion
#region 第四题
//打印2+4+6+…+100的值
int count = 0;
int num = 2;
while (num < 101)
{
count += num;
num += 2;
}
Console.WriteLine(count);
#endregion
#region 第五题 ---------水仙花数
//水仙花数:是⼀个各个位的⽴⽅之和等于该整数的三位数
int num = 100;
int count = 0;
while (num < 1000)
{
int a = num % 10;
int b = (num / 10) % 10;
int c = (num / 100) % 10;
if (num == a * a * a + b * b * b + c * c * c)
{
Console.WriteLine(num);
count++;
}
num++;
}
Console.WriteLine(count);
#endregion
#region 第六题 -------判断质数
//质数:只能被1和它本身整除的数是质数
//0和1既不是质数也不是和数
//1.标准版:判断从2到sqrt(n)是否存在其约数,时间复杂度O(sqrt(n))
//2.高配版:判断2之后,就可以判断从3到sqrt(n)之间的奇数了,无需再判断之间的偶数,时间复杂度O(sqrt(n)/2)
//3.VIP尊享版:质数分布的规律,大于等于5的质数一定和6的倍数相邻。
//证明:令x≥1,将大于等于5的自然数表示如下:
///··· 6x-1,6x,6x+1,6x+2,6x+3,6x+4,6x+5,6(x+1),6(x+1)+1 ···
///可以看到,不和6的倍数相邻的数为6x+2,6x+3,6x+4,由于2(3x+1),3(2x+1),2(3x+2),所以它们一定不是素数,
///再除去6x本身,显然,素数要出现只可能出现在6x的相邻两侧。因此在5到sqrt(n)中每6个数只判断2个,时间复杂度O(sqrt(n)/3)。
Console.WriteLine("请输入一个整数:");
int num = int.Parse(Console.ReadLine());
int i = 2;
while (i < num)
{
if (num % i == 0)
{
Console.WriteLine("不是质数");
break;
}
i++;
}
if (i == num)
{
Console.WriteLine("是质数");
}
#endregion
#region 第七题 ------------出租车收费(分段函数)
//某城市普通出租⻋收费标准如下:"起步⾥程3公⾥,起步费10元;超起步⾥程后10
//公⾥内,每公⾥租费2元,超过10公⾥以上的部分加收50%的回空补贴费,即每公⾥租费3
//元。营运过程中,因路阻及乘客要求临时停⻋的,每5分钟按1公⾥租费计收(不⾜5分钟的算
//做5分钟)。运价计费尾数四舍五⼊,保留到元。"。编写程序,输⼊⾏驶⾥程(公⾥)与等待时
//间(分钟),计算并输出乘客应⽀付的⻋费(元)。
Console.WriteLine("请输入行驶里程:");
int distance = int.Parse(Console.ReadLine());
Console.WriteLine("请输入等待时间:");
int waitingTime = int.Parse(Console.ReadLine());
if (waitingTime >= 5)
{
distance = distance + waitingTime / 5;
}
else
{
distance = distance + 1;
}
int money = 10;
if (distance > 3 && distance <= 13)
{
money = 10 + (distance - 3) * 2;
}
else if (distance > 13)
{
money = 10 + 10 * 2 + (distance - 13) * 3;
}
else
{
Console.WriteLine("输入有误");
}
Console.WriteLine($"乘客应支付:{money}元");
#endregion*/
}
}
}