C# 语法基础(关系,逻辑,条件,赋值运算符)
关系运算符:> < >= <= 以及 == (等于) !=(不等于)
逻辑运算符:&&(与) ||(或) !(非,优先级最高)
赋值运算符:= += -= *= /= %= 例:x-=4即x=x-4
条件运算符: ? A :B 如果是true 执行A,否则执行B
举几个例子:
①7的倍数,十位数是7的数,个位数是7的数
Console.WriteLine("请输入100以内的数");
int a = int.Parse(Console.ReadLine());
Console.WriteLine(a % 7 == 0); //被7整除的数
Console.WriteLine(a / 10 == 7);//十位数是7
Console.WriteLine(a %10 == 7);//个位数是7
Console.ReadKey();
②
Console.WriteLine("请输入2个汉字");
string a1,a2;
a1 = Console.ReadLine();
a2 = Console.ReadLine();
Console.WriteLine(a1 == "你好");
Console.WriteLine(a1 == "你好" && a2 == "同学");
Console.WriteLine(a1 == "你好" || a2 == "同学");
Console.WriteLine(!(a1 == "你好" && a2 == "同学"));
Console.ReadKey();
③
Console.WriteLine("请输入2个10以内的数值且相加等于15");
int b2 = int.Parse(Console.ReadLine());
int b3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(b2 + b3 ==15 ? "回答正确" : "回答错误,拖出去阉了!");
Console.ReadKey();
④
标准体重:
男士体重-身高+100=±3;女士体重-身高+110=±3;
string xb;
double tz, sg;
Console.WriteLine("判断一个人的体重是否是标准体重");
Console.Write("性别:");
xb = Console.ReadLine();
Console.Write("体重(kg):");
tz = double.Parse(Console.ReadLine());
Console.Write("身高(cm):");
sg = Convert.ToDouble(Console.ReadLine());
int y = xb == "男" ? 100 : 110;
Console.WriteLine(tz-sg+y<-3?"太瘦":(tz-sg+y<=3)?"完美":"太胖");
Console.ReadKey();
⑤
int b = 1, c = 1, d = 1, e = 1, f = 1;
b += 3;
c -= 3;
d *= 3;
e /= 3;
f %= 3;
Console.WriteLine(b.ToString());
Console.WriteLine(c.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(e.ToString());
Console.WriteLine(f.ToString());
Console.ReadKey();