内容:运算符
一·分类
1算数,有 ++(加加),——(减减);*(乘),/(除),%(取余);+(加),—(减);(以段号分为三组运算优先依次递减)
1)++:a++ , ++a, --a, a-- *++a,--a这种格式时,++,--要优先运算 而a++,a-- 是先运算完其他指令再运算。例int a=1 int b=2
b=++a时b=a+1=2运算完成后 但a=2,而b=a++时 b=a b=1 但a在运算完之后a=2
2关系,链条记忆 等于(==)→不等于(!=(注意中间无空格))→大于(>)→小于(<)→大于等于(>=)→小于等于(<=)运算结果为波尔型即true,false
3逻辑,与(&&) ,或(||),非(!)→在这些运算符中为最优先。
4条件运算符,(?:)见下
5赋值 , = += -= *= /= %= 例:a+=2 就是a+2=a
vs:记录
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
1)static void Main(string[] args)
{
string fangzi = "";
string piaozi = "";
string nengli = "";
string jieGuo = "";
//输入
Console.Write("姑娘:有房子吗?");
fangzi = Console.ReadLine();
Console.Write("姑娘:有钱吗?");
piaozi = Console.ReadLine();
Console.Write("姑娘:有能力吗?");
nengli = Console.ReadLine();
//运算
jieGuo = fangzi == "有" ? "咱们结婚吧" : (piaozi == "有" ? "买房子结婚吧" : (nengli == "有" ? "快点赚钱买房子结婚吧" : "去学IT吧"));
//输出
Console.WriteLine(jieGuo);
}
2)static void Main555(string[] args)
{
string sex = "";
int age;
string jieGuo = "";
//输入
Console.Write("性别:");
sex = Console.ReadLine();
Console.Write("年龄:");
age = Convert.ToInt32(Console.ReadLine());
//运算
jieGuo = sex=="女"?"回家看门去":(age>=18&&age<=60?"抓去当兵":(age<18?"回家吃奶去":"回家养老吧"));
//输出
Console.WriteLine(jieGuo);
}
3)static void Main(string[] args)
{
string shour="";
int hour = 0;
string ap = "";
string wenhou = "";
//输入
Console.Write("请输入当前小时:");
shour = Console.ReadLine();
//计算
hour = Convert.ToInt32(shour); //类型转换
wenhou = hour>=0&&hour<6?"晚上没有睡吗?":(hour>=6&&hour<11?"上午好!":(hour >=11&&hour<14?"中午好":(hour>=14&&hour<18?"下午好":(hour>=18&&hour<22?"晚上好":(hour>=22&&hour<=23?"太晚了,早点休息":"时间不对啊")))));
ap = hour > 12 ? "PM" : "AM";
hour = hour > 12 ? hour - 12 : hour; //时制转换
//输出
Console.WriteLine("当前时间是:{0}{1}\n{2}",hour,ap,wenhou);
}
4)static void Main3333(string[] args)
{
//24小时,12小时
int hour = 14;
string ap = "";
ap = hour > 12 ? "PM" : "AM";
hour = hour>12?hour-12:hour;
Console.WriteLine(hour+ap);
//int a = 10;
//int b = 15;
//int c=0;
//c = a > b ? a : b;
////int c = a > b ? a : b;
//Console.WriteLine(c);
//int a = 10;
//int b = 5;
//a += b; //a = a + b;
//Console.WriteLine(a);
//int a = 10;
//int b = 5; // 0<a<15
//Console.WriteLine(a > 0 || a < 5);
//int a = 10;
////++a; --a; a--; a = a - 1;
////a++; a = a + 1;
//int c = --a;
//int b = a--;
//Console.WriteLine("a={0}", a);
//Console.WriteLine("b={0}", b);
//Console.WriteLine("c={0}", c);
}
5)static void Main222(string[] args)
{
const double PI =3.14;
string s = "2ello world";
int a = 10;
int b;
b = 30;
//a + b = 30; //错
//20 = b; //错
Console.WriteLine(PI);
//Console.WriteLine(b);
}
}
}