关于 if swithch 分支语句 及相关练习

69 阅读1分钟

// See aka.ms/new-console… for more information Console.WriteLine("Hello, World!"); //Console.WriteLine(); 有默认的换行符 /* console.write() // 没有自带的换行符

逻辑运算符 : && || !

赋值运算符优先级最低!

三目运算符 式子?值1:值2 当式子为真时,那么就等于值1,否则就是值2

*/ //Console.WriteLine(true && false);

// 类型转换 强转 convert /* string str = "2"; int num = 1; num = Convert.ToInt32(str); Console.WriteLine(num);

/ / 条件分支 双分支 两种情况 if(判断条件) { 执行语句1 当条件为真时会执行语句1 } else{ 执行语句2 }

多分支 if() {

} else if() {

} else if() {

} else{} / /int num1 = 3; if (num1 > 3) { Console.WriteLine("1"); } else if (num1 > 2) { Console.WriteLine("2"); } else { Console.WriteLine("3"); }/ /int a = 70; Console.WriteLine("qing shuru kao shi chengji :"); int score = Convert.ToInt32(Console.ReadLine()); if (score <= 100 && score >= 90) { Console.WriteLine("A"); } else if (score >= 70 && score <= 89) { Console.WriteLine("B"); } else { Console.WriteLine("C"); }/ / 判断象限: x,y x=0 y=0 原点 x >0 y >0 第一象限 ......

*/

/*Console.WriteLine("请输入x坐标的值"); int x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入y坐标的值"); int y = Convert.ToInt32(Console.ReadLine()); if (x == 0 && y == 0) {

Console.WriteLine("原点");

} else if (x > 0 && y <0) { Console.WriteLine("第四象限"); } else if(x > 0 && y > 0) { Console.WriteLine("第一象限");

} else if (x < 0 && y > 0) { Console.WriteLine("第二象限");

} else { Console.WriteLine("第三象限"); }*/

/* 输入switch 分支语句 switch(比对的值) 值1 值2 都是执行同一个语句 { case 值1: case 值2: 执行语句; break; case 值3: 执行语句; break; default: 执行语句 break; } */ // 每星期的日程 /string day = Console.ReadLine(); switch( day ) { case "星期一": case "monday": Console.WriteLine("去上课"); break; case "星期二": Console.WriteLine("上半天课"); break; case "星期三": Console.WriteLine("烦死了"); break; case "星期四": Console.WriteLine("快放假了"); break; case "星期五": Console.WriteLine("最后一天"); break; default: Console.WriteLine("已经在愉快的度过周六周日呢"); break; }/ int a = Convert.ToInt32(Console.ReadLine()); int max = a; int min = a; int b = Convert.ToInt32(Console.ReadLine()); if (b > max) max = b; if (b < min) min = b; int c = Convert.ToInt32(Console.ReadLine()); if (c > max) max = c; if (c < min) min = c; int d = Convert.ToInt32(Console.ReadLine()); if (d > max) max= d; if (d < min) min= d; Console.WriteLine("max is{0}.min is {1} ",max,min); /*if(a > b && a >c && a>d) { Console.WriteLine("max is:", a); } else if (b >a && b > c && b > d) { Console.WriteLine(b); } else if (c > a && c > b && c > d) { Console.WriteLine(c); } else if(d > a && d > b && d > c) { Console.WriteLine(d); }

if (a < b && a < c && a < d) { Console.WriteLine(a); } else if (b < a && b < c && b < d) { Console.WriteLine(b); } else if (c < a && c < b && c < d) { Console.WriteLine(c); } else if (d < a && d <b && d < c) { Console.WriteLine(d); }*/