1、类型如果相兼容的两个变量,可以使用自动类型转换或者强制类型转换, 但是,如果两个类型的变量不兼容,比如 string与int或者string 与double, 这个时候我们可以使用一个叫做Convert的转换工厂进行转换。 注意:使用Convert进行类型转换,也需要满足一个条件: 面儿上必须要过的去。
2、算数运算符 ++:分为前++和后++,不管是前++还是后++,最终的结果都是给这个变量加一。 区别表现表达式当中,如果是前++,则先给这个变量自身加一,然后带着这个加一后的值去参与运算。 如果是后++,则先拿原值参与运算,运算完成后,再讲这个变量自身加一。 --:同上。
3、 对于向加加或者减减这样只需要一个操作数就能完成的运算,我们称之为一元运算符。 + - * / % 对于这些需要两个或以上才能完成运算的操作符,我们称之为二元运算符。 一元运算符的优先级要高于而元运算符。 如果在一个表达式当中,既有一元运算符,又有二元运算符,我们首先计算一元运算符。
int number=10; int result=10 + ++number;
4、关系运算符 > < >= <= == != 关系运算符是用来描述两个事物之间的关系 由关系运算符连接的表达式称之为关系表达式。 5、bool类型 在c#中我们用bool类型来描述对或者错。 bool类型的值只有两个 一个true 一个false
6、逻辑运算符 && 逻辑与 ||逻辑或 !逻辑非 又逻辑运算符连接的表达式叫做逻辑表达式
逻辑运算符两边放的一般都是关系表达式或者bool类型的值。 5>3 &&true 3>5||false !表达式 逻辑表达式的结果同样也是bool类型
7、复合赋值运算符 int number=10; += : number+=20; number=number+20; -= number-=5; number=number-5; *= number*=5; number=number*5; /= %=
中级程序员 --2年 ---高级程序员---->小组组长---> 项目经理 业务经理 产品经理 高级程序员 不明觉厉 软件开发工程师
顺序结构:程序从Main函数进入,从上到下一行一行的执行,不会落下任何一行。 分支结构:if if-else 选择结构:if else-if switch-case 循环结构:while do-while for foreach
8、 if语句: 语法: if(判断条件) { 要执行的代码; } 判断条件:一般为关系表达式或者bool类型的值。 执行过程:程序运行到if处,首先判断if所带的小括号中的判断条件, 如果条件成立,也就是返回true,则执行if所带的大括号中的代码, 如果判断条件不成立,也就是返回一个false。则跳过if结构,继续向下执行。
if结构的特点:先判断,再执行,有可能一行代码都不执行 用于一种情况的判断。
9、if-else 语法: if(判断条件) { 执行的代码; } else { 执行的代码 } 执行过程:程序执行到if处,首先判断if所带的小括号中的判断条件是否成立, 如果成立,也就是返回一个true,则执行if所带的大括号中的代码, 执行完成后,跳出if-else结构。 如果if所带的小括号中的判断条件不成立,也就是返回一个false, 则跳过if语句,执行else所带的大括号中的语句,执行完成后,跳出if-else结构。
if-else特点:先判断,再执行,最少都要执行一条代码。 用于两种情况的判断
注意:else永远跟离它最近的那个if配对
10、if else-if 作用:用来处理多条件的区间性的判断。 语法: if(判断条件) { 要执行的代码; } else if(判断条件) { 要执行的代码; } else if(判断条件) { 要执行的代码; } else if(判断条件) { 要执行的代码; } ........ else { 要执行的代码; } 执行过程;程序首先判断第一个if所带的小括号中的判断条件,如果条件成立,也就是返回一个true, 则执行该if所带的大括号中的代码,执行完成后,立即跳出if else-if结构。 如果第一个if所带的判断条件不成立,也就是返回一个false,则继续向下进行判断,依次的判断每一个if所带 的判断条件,如果成立,就执行该if所带的大括号中的代码,如果不成立,则继续向下判断, 如果每个if所带的判断条件都不成立,就看当前这个if else-if结构中是否存在else。 如果有else的话,则执行else中所带的代码,如果没有else,则整个 if-else if神马都不做。 else可以省略。
01复习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _01复习 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 /*14 变量15 * 16 * 赋值运算符= int num=10;17 * 占位符 18 * 变量的命名规范19 * Camel:20 * Pascal:21 * +号的使用:22 * 1、连接 23 * string s1="123";24 * int num=5;25 * s1+num num+10026 * 2、相加27 * 三种注释28 * //29 * ///30 * 快捷键31 * 算数运算符32 * +=*除%33 * 转义符34 * // /" /r/n /b /t 35 * @ 1、取消\在字符串中的转义作用 2、按原格式输出字符串36 * 类型转换37 * 1、强制类型转换 显示类型转换38 * 2、自动类型转换 隐式类型转换39 * 40 * 类型兼容 double int41 * 42 * 43 44 */45 //int num = 10;46 //double d = num;//自动 小的转大的47 48 49 //double dd = 3.13;50 //int n = (int)dd;51 }52 }53 }
02、两道作业题
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _02_两道作业题 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //练习,编程实现计算几天(如46天)是几周零几 天. 6周零4天14 //int days = 46;15 //int weeks = days / 7;16 //int day = days % 7;17 //Console.WriteLine("{0}天是{1}周零{2}天",days,weeks,day);18 //Console.ReadKey();19 20 21 //编程实现107653秒是几天几小时几分钟几秒?22 23 24 // 60*60 3600 *24=86400 8640025 26 int seconds = 107653;27 int days = seconds / 86400;//求得天数28 int secs = seconds % 86400;//求得求完天数后剩余的秒数29 int hours = secs / 3600;//求得小时数30 secs = secs % 3600;//求得小时数后剩余的秒数31 int mins = secs / 60;//求得分钟数32 secs = secs % 60;33 34 Console.WriteLine("{0}秒是{1}天{2}小时{3}分钟{4}秒", seconds, days, hours, mins, secs);35 Console.ReadKey();36 }37 }38 }
03类型转换
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _03类型转换 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //显示类型转换、自动类型转换14 //int--double double ----int15 16 //string s = "123abc";17 ////将字符串转换成int或者double类型18 //double d = Convert.ToDouble(s);19 //int n = Convert.ToInt32(s);20 //Console.WriteLine(n);21 //Console.WriteLine(d);22 //Console.ReadKey();23 24 25 //让用户输入姓名 语文 数学 英语 三门课的成绩,26 //然后给用户显示:XX,你的总成绩为XX分,平均成绩为XX分。27 Console.WriteLine("请输入你的姓名");28 string name = Console.ReadLine();29 Console.WriteLine("请输入你的语文成绩");30 string strChinese = Console.ReadLine();31 Console.WriteLine("请输入你的数学成绩");32 string strMath = Console.ReadLine();33 Console.WriteLine("请输入你的英语成绩");34 string strEnglish = Console.ReadLine();35 36 37 double chinese = Convert.ToDouble(strChinese);38 double math = Convert.ToDouble(strMath);39 double english = Convert.ToDouble(strEnglish);40 41 double sumScore = chinese + math + english;42 double avg = (int)sumScore*1.0 / 3;43 Console.WriteLine("{0}你的总成绩是{1}平均成绩是{2:0.00}", name, sumScore, avg);44 Console.ReadKey();45 46 //55 77 88 55778847 //由于字符串去相加的话,最终会变成相连接,如果要拿字符串类型的变量参与计算48 //需要将字符串转换成int或者double类型49 //int chinese = Convert.ToInt32(strChinese);50 //int math = Convert.ToInt32(strMath);51 //int english = Convert.ToInt32(strEnglish);52 53 //Console.WriteLine("{0}你的总成绩是{1},平均成绩是{2}", name, chinese + math + english, (chinese + math + english) / 3);54 //Console.ReadKey();55 56 }57 }58 }
04Convert类型转换
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _04Convert类型转换 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //提示用户输入一个数字 接收 并且向控制台打印用户输入的这个数字的2倍14 Console.WriteLine("请输入一个数字");15 // string strNumber = Console.ReadLine();16 //将用户输入的字符串转换成int或者double类型17 double number = Convert.ToDouble(Console.ReadLine());18 Console.WriteLine(number*2);19 Console.ReadKey();20 21 }22 }23 }
05加加减减
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _05加加减减 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 // int number = 10;14 // // number++;15 //// ++number;//number=number+1;16 // // number--;17 // --number;18 // Console.WriteLine(number);19 // Console.ReadKey();20 21 // int number = 10;22 //// int result = 10 + number++;23 // //int result = 10 + number;24 // //number++;25 26 27 28 // //int result = 10 + ++number;29 // number++;30 // int result = 10 + number;31 // Console.WriteLine(number);32 // Console.WriteLine(result);33 // Console.ReadKey();34 35 36 // int number = 10;37 //// int result = 10 + number--;38 // //int result = 10 + number;39 // //number--;40 41 // // int result = 10 + --number;42 // number--;43 // int result = 10 + number;44 // Console.WriteLine(number);45 // Console.WriteLine(result);46 // Console.ReadKey();47 48 // int a = 5;49 // a++;50 // ++a;51 // --a;52 // a--;53 // int b = a++ + ++a * 2 + --a + a++;54 //// int b = a + a * 2 + a + a;55 // // 5+7*2+6+6 756 // Console.WriteLine(a);57 // Console.WriteLine(b);58 // Console.ReadKey();59 60 //如果你有了一个喜欢的女生,你应该好好学习,努力上个好大学,毕业找个高新的工作。61 //在你喜欢的女生结婚的时候 多随点份子钱。62 63 }64 }65 }
06关系运算符
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _06关系运算符 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 // 大象的重量(1500)>老鼠的重量(1)14 //关系表达式的结果是bool类型15 // bool b = 1500 > 1;16 // bool b = 3 > 1000;17 // bool b = 39 < 18;18 bool b = 20 == 20;19 Console.WriteLine(b);20 Console.ReadKey();21 //兔子的寿命(3)>乌龟的寿命(1000)22 //39<1823 //我的年龄(20)==你的年龄(20)24 25 }26 }27 }
07逻辑运算符练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _07逻辑运算符练习 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //让用户输入老苏的语文和数学成绩,输出以下判断是否正确,正确输出True,错误输出False14 //1)老苏的语文和数学成绩都大于90分15 Console.WriteLine("小苏,输入你的语文成绩");16 //string strChinese = Console.ReadLine();17 int chinese = Convert.ToInt32(Console.ReadLine());18 Console.WriteLine("小苏,请输入你的数学成绩");19 int math = Convert.ToInt32(Console.ReadLine());20 21 22 //bool b = chinese > 90 && math > 90;23 bool b = chinese > 90 || math > 90;24 Console.WriteLine(b);25 Console.ReadKey();26 27 28 29 //2)语文和数学有一门是大于90分的30 31 }32 }33 }
08判断闰年
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _08判断闰年 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //Console.WriteLine("请输入要判断的年份");14 //int year = Convert.ToInt32(Console.ReadLine());15 ////年份能够被400整除.(2000)16 ////年份能够被4整除但不能被100整除.(2008)17 18 19 ////逻辑与的优先级要高于逻辑或20 //bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);21 22 //Console.WriteLine(b);23 //Console.ReadKey();24 25 26 27 // bool b = 5 < 3 && 5 > 3;28 29 bool b = 5 > 3 || 4 < 3;30 31 }32 }33 }
09上午小复习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _09上午小复习 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 /*14 变量类型:int double string char decimal bool15 * 运算符:16 * 赋值运算符:=17 * 复合赋值运算符:+= -= *= /= %=18 * 算数运算符:+ - * / % ++ --19 关系运算符: > < >= <= == !=20 * 逻辑运算符: && || !21 * 类型转换:22 * 1、自动类型转换 小的转大的 int--->double23 * 2、强制类型转换 大的转小的 double-->int24 * 3、Convert25 */26 }27 }28 }
10if结构
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10if结构 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //编程实现:如果跪键盘的时间大于60分钟,那么媳妇奖励我晚饭不用做了14 Console.WriteLine("请输入你跪键盘的时间");15 int mins = Convert.ToInt32(Console.ReadLine());16 17 //如果跪键盘的时间>60分钟 则不做晚饭18 19 bool b = mins > 60;20 //如果你想表示的含义是当b的值为true的时候去执行if中代码,21 //那么 语法上 ==true可以省略22 //但是,如果你想表示的是当b==false的时候去执行if中代码,23 //语法上 ==false不能省略24 if (mins>60)25 {26 Console.WriteLine("好老公,不用跪键盘了,去吃屎吧");27 }28 Console.ReadKey();29 30 31 }32 }33 }
11if结构的3个练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11if结构的3个练习 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //让用户输入年龄,如果输入的年龄大于23(含)岁,则给用户显示你到了结婚的年龄了.14 15 //Console.WriteLine("请输入你的年龄");16 //int age = Convert.ToInt32(Console.ReadLine());17 //bool b = age >= 23;18 //if (b)19 //{20 // Console.WriteLine("你可以结婚啦");21 //}22 //Console.ReadKey();23 24 //如果老苏的(chinese music)25 //语文成绩大于90并且音乐成绩大于8026 //语文成绩等于100并且音乐成绩大于70,则奖励100元.27 //Console.WriteLine("请输入老苏的语文成绩");28 //int chinese = Convert.ToInt32(Console.ReadLine());29 //Console.WriteLine("请输入老苏的音乐成绩");30 //int music = Convert.ToInt32(Console.ReadLine());31 32 //bool b = (chinese > 90 && music > 80) || (chinese == 100 && music > 70);33 34 //if (b)35 //{36 // Console.WriteLine("奖励100元");37 //}38 //Console.ReadKey();39 40 //让用户输入用户名和密码,如果用户名为admin,密码为888888,则提示登录成功.41 Console.WriteLine("请输入用户名");42 string name = Console.ReadLine();43 Console.WriteLine("请输入密码");44 string pwd = Console.ReadLine();45 46 if (name == "admin" && pwd == "mypass")47 {48 Console.WriteLine("登陆成功");49 }50 Console.ReadKey();51 52 53 }54 }55 }
12if-else练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12if_else练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //如果小赵的考试成绩大于90(含)分,那么爸爸奖励他100元钱, 14 //否则的话,爸爸就让小赵跪方便面. 15 //Console.WriteLine("请输入小赵的考试成绩"); 16 //int score = Convert.ToInt32(Console.ReadLine()); 17 //if (score >= 90) 18 //{ 19 // Console.WriteLine("奖励你一百块"); 20 //} 21 //else 22 //{ 23 // Console.WriteLine("去跪方便面"); 24 //} 25 //Console.ReadKey(); 26 27 //对学员的结业考试成绩评测 28 // 成绩>=90 :A 29 //90>成绩>=80 :B 30 //80>成绩>=70 :C 31 //70>成绩>=60 :D 32 // 成绩<60 :E 33 34 35 Console.WriteLine("请输入学员的考试成绩"); 36 int score = Convert.ToInt32(Console.ReadLine()); 37 //最正确的做法 38 39 if (score >= 90) 40 { 41 Console.WriteLine("A"); 42 } 43 else if (score >= 80) 44 { 45 Console.WriteLine("B"); 46 } 47 else if (score >= 70) 48 { 49 Console.WriteLine("C"); 50 } 51 else if (score >= 60) 52 { 53 Console.WriteLine("D"); 54 } 55 //else if (score < 60) 56 //{ 57 // Console.WriteLine("E"); 58 //} 59 else 60 { 61 Console.WriteLine("E"); 62 } 63 64 Console.ReadKey(); 65 66 67 68 69 #region if的做法 70 //if (score >= 90 && score < 100) 71 //{ 72 // Console.WriteLine("A"); 73 //} 74 //if (score >= 80 && score < 90)//ctrl+k+d 75 //{ 76 // Console.WriteLine("B"); 77 //} 78 //if (score >= 70 && score < 80) 79 //{ 80 // Console.WriteLine("C"); 81 //} 82 //if (score >= 60 && score < 70)//98 88 83 //{ 84 // Console.WriteLine("D"); 85 //} 86 ////else 87 ////{ 88 //// Console.WriteLine("E"); 89 ////} 90 //if (score < 60) 91 //{ 92 // Console.WriteLine("E"); 93 //} 94 #endregion 95 #region if else-if 96 //if (score >= 90) 97 //{ 98 // Console.WriteLine("A"); 99 //}100 //else//<90 101 //{102 // if (score >= 80)103 // {104 // Console.WriteLine("B");105 // }106 // else//<80107 // {108 // if (score >= 70)109 // {110 // Console.WriteLine("C");111 // }112 // else//<70113 // {114 // if (score >= 60)115 // {116 // Console.WriteLine("D");117 // }118 // else//<60119 // {120 // Console.WriteLine("E");121 // }122 // }123 // }124 //} 125 #endregion126 Console.ReadKey();127 128 }129 }130 }
13、练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13_练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //比较3个数字的大小 不考虑相等 14 15 //分别的提示用户输入三个数字 我们接受并且转换成int类型 16 //Console.WriteLine("请输入第一个数字"); 17 //int numberOne = Convert.ToInt32(Console.ReadLine()); 18 //Console.WriteLine("请输入第二个数字"); 19 //int numberTwo = Convert.ToInt32(Console.ReadLine()); 20 //Console.WriteLine("请输入第三个数字"); 21 //int numberThree = Convert.ToInt32(Console.ReadLine()); 22 23 24 //三种情况 应该使用 if else-if来做 25 //如果第一个数字大于第二个数字 并且第一个数字还大于第三个数字 26 //if (numberOne > numberTwo && numberOne > numberThree) 27 //{ 28 // Console.WriteLine(numberOne); 29 //} 30 ////如果第二个数字大于第一个数字并且第二个数字大于第三个数字 31 //else if (numberTwo > numberOne && numberTwo > numberThree) 32 //{ 33 // Console.WriteLine(numberTwo); 34 //} 35 ////如果第三个数字大于第一个数字并且第三个数字大于第二个数字 36 //else 37 //{ 38 // Console.WriteLine(numberThree); 39 //} 40 41 42 //我先让第一个数字跟第二个数字比较 如果大于第二个数字 再让第一个数字跟第三个数字比较 43 //if (numberOne > numberTwo) 44 //{ 45 // //如果第一个数字大于了第二个数字 再让第一个数字跟第三个数字比较 46 // if (numberOne > numberThree) 47 // { 48 // Console.WriteLine(numberOne); 49 // } 50 // else//第三个数字要大于第一个数字 51 // { 52 // Console.WriteLine(numberThree); 53 // } 54 //} 55 //else//第二个数字大于了第一个数字 56 //{ 57 // //让第二个数字跟第三个数字进行比较 如果第二个数字大于第三个数字 第二个数字最大 否则第三个数字最大 58 // if (numberTwo > numberThree) 59 // { 60 // Console.WriteLine(numberTwo); 61 // } 62 // else//第三个数字最大 63 // { 64 // Console.WriteLine(numberThree); 65 // } 66 //} 67 68 69 70 //练习1:提示用户输入密码,如果密码是“88888”则提示正确,否则要求再输入一次, 71 //如果密码是“88888”则提示正确,否则提示错误,程序结束。 72 //(如果我的密码里有英文还要转换吗,密码:abc1) 73 74 //Console.WriteLine("请输入密码"); 75 //string pwd = Console.ReadLine(); 76 //if (pwd == "888888") 77 //{ 78 // Console.WriteLine("登陆成功"); 79 //} 80 //else//要求用户再输入一次 81 //{ 82 // Console.WriteLine("密码错误,请重新输入"); 83 // pwd = Console.ReadLine(); 84 // if (pwd == "888888") 85 // { 86 // Console.WriteLine("输了两遍,终于正确了"); 87 // } 88 // else//输入第二次错误 89 // { 90 // Console.WriteLine("两边都不对,程序结束"); 91 // } 92 //} 93 94 //Console.ReadKey(); 95 96 97 98 99 //练习2:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,100 //则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,101 //如果用户名是admin则提示密码错误.102 //Console.WriteLine("请输入用户名");103 //string name = Console.ReadLine();104 //Console.WriteLine("请输入密码");105 //string pwd = Console.ReadLine();106 107 108 ////第一种情况:用户名和密码全部输入正确109 //if (name == "admin" && pwd == "888888")110 //{111 // Console.WriteLine("登陆成功");112 //}113 ////第二种情况:密码错误114 //else if (name == "admin")115 //{116 // Console.WriteLine("密码输入错误,程序退出");117 //}118 ////第三种情况:用户名错误119 //else if (pwd == "888888")120 //{121 // Console.WriteLine("用户名错误,程序退出");122 //}123 ////第四种情况:用户名和密码全部错误124 //else125 //{126 // Console.WriteLine("用户名和密码全部错误,程序退出");127 //}128 129 130 131 //练习3:提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,132 //则告知不允许查看,如果大于等于10岁并且小于18,133 //则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,134 //否则提示"退出,你放弃查看"。135 136 //第一种情况 >=18 137 //第二种情况 <10138 //第三种情况 >=10 && <18139 140 Console.WriteLine("请输入你的年龄");141 int age = Convert.ToInt32(Console.ReadLine());142 143 if (age >= 18)144 {145 Console.WriteLine("看吧,早晚你都要知道的");146 }147 else if (age < 10)148 {149 Console.WriteLine("滚蛋,回家吃奶去");150 }151 else152 {153 Console.WriteLine("确定要看么?yes/no");154 //input 要么是yes要么是no155 string input = Console.ReadLine();156 if (input == "yes")157 {158 Console.WriteLine("看吧,早熟的孩子,后果自负哟");159 }160 else//no161 {162 Console.WriteLine("乖孩子,回家吃奶去吧");163 }164 }165 166 Console.ReadKey();167 168 }169 }170 }