程序调试 for循环 三元表达式


1、*程序调试 1)、写完一段程序后,想看一下这段程序的执行过程。 2)、当你写完这段程序后,发现,程序并没有按照你想象的样子去执行。

调试方法: 1)、F11逐语句调试(单步调试) 2)、F10逐过程调试 3)、断点调试

2、for循环 语法: for(表达式1;表达式2;表达式3) {  循环体; } 表达式1一般为声明循环变量,记录循环的次数(int i=0;) 表达式2一般为循环条件(i<10) 表达式3一般为改变循环条件的代码,使循环条件终有一天不再成立(i++)。 执行过程:程序首先执行表达式1,声明了一个循环变量用来记录循环的次数, 然后执行表达式2,判断循环条件是否成立,如果表达式2返回的结果为true, 则执行循环体。当执行完循环体后,执行表达式3,然后执行表达式2继续判断循环条件是否成立, 如果成立则继续执行循环体,如果不成立,则跳出for循环。

3、int.TryParse int.parse 尝试着将一个字符串转换成int类型。

4、三元表达式 语法: 表达式1?表达式2:表达式3; 表达式1一般为一个关系表达式。 如果表达式1的值为true,那么表达式2的值就是整个三元表达式的值。 如果表达式1的值为false,那么表达式3的值就是整个三元表达式的值。 注意:表达式2的结果类型必须跟表达式3的结果类型一致,并且也要跟整个三元表达式的结果类型一致。

for循环

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03for循环
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //向控制台打印10遍  欢迎来到传智播客.Net学习
14 
15             for (int i = 0; i < 10; i++)
16             {
17                 Console.WriteLine("欢迎来到传智播客.Net学习{0}", i);
18             }
19             //for (int i = 0; i < length; i++)
20             //{
21 
22             //}
23             Console.ReadKey();
24 
25             //int i = 0;//定义循环的次数
26             //while (i < 10)
27             //{
28             //    Console.WriteLine("欢迎来到传智播客.Net学习");
29             //    i++;
30             //}
31             //Console.ReadKey();
32         }
33     }
34 }

for循环的练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _05for循环的练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //求1-100之间的所有整数和   偶数和  奇数和
14             //int sum = 0;
15             //int n = 100;
16             //for (int i = 1; i <= n; i += 2)
17             //{
18             //    sum += i;
19             //}
20             //Console.WriteLine(sum);
21             //Console.ReadKey();
22 
23             //找出100-999间的水仙花数?
24             //水仙花数指的就是 这个百位数字的
25             //百位的立方+十位的立方+个位的立方==当前这个百位数字
26             //153  1 125  27  153  i
27             //百位:153/100
28             //十位:153%100/10
29             //个位:153%10
30 
31             for (int i = 100; i <= 999; i++)
32             {
33                 int bai = i / 100;
34                 int shi = i % 100 / 10;
35                 int ge = i % 10;
36                 if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
37                 {
38                     Console.WriteLine("水仙花数有{0}",i);
39                 }
40             }
41             Console.ReadKey();
42 
43         }
44     }
45 }

for循环的嵌套

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _06for循环的嵌套
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14 
15             //当遇到某个事情要做一遍,而另外一个事情要做N遍的时候
16             //for循环的嵌套
17             for (int i = 0; i < 10; i++)
18             {
19                 for (int j = 0; j < 10; j++)
20                 {
21                     Console.WriteLine("Hello World i循环了{0}次,j循环了{1}次",i,j);
22                     break;
23                 }
24             }
25             Console.ReadKey();
26         }
27     }
28 }

乘法口诀表

 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 Program
10     {
11         static void Main(string[] args)
12         {
13             //for (int i = 1; i <= 9; i++)
14             //{
15             //    for (int j = 1; j <= i; j++)
16             //    {
17             //        Console.Write("{0}*{1}={2}\t", i, j, i * j);
18             //    }
19             //    Console.WriteLine();//换行
20             //}
21             //Console.ReadKey();
22 
23             //Console.Write("Hello Wor\tld");
24             //Console.WriteLine();
25             //Console.Write("Hello World");
26             //Console.ReadKey();
27 
28             Console.WriteLine("请输入一个数字");
29             int number = Convert.ToInt32(Console.ReadLine());
30 
31 
32             for (int i = 0; i <=number; i++)
33             {
34                 Console.WriteLine("{0}+{1}={2}",i,number-i,number);
35             }
36             Console.ReadKey();
37         }
38     }
39 }

类型转换

 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 Program
10     {
11         static void Main(string[] args)
12         {
13             //使用Convert进行转换 成功了就成了, 失败了就抛异常
14             // int numberOne = Convert.ToInt32("123abc");
15 
16             // int number = int.Parse("123abc");
17 
18             //Console.WriteLine(number);
19             int number = 100;
20             //参数 返回值
21             bool b = int.TryParse("123abc", out number);
22             Console.WriteLine(b);
23             Console.WriteLine(number);
24             //方法 或者 函数?
25             Console.ReadKey();
26         }
27     }
28 }

for循环的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 _09for循环的3个练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //练习1:循环录入5个人的年龄并计算平均年龄,
14             //如果录入的数据出现负数或大于100的数,立即停止输入并报错.
15             //int sum = 0;
16             //bool b = true;
17             //for (int i = 0; i < 5; i++)
18             //{
19             //    Console.WriteLine("请输入第{0}个人的成绩",i+1);
20             //    try
21             //    {
22             //        int age = Convert.ToInt32(Console.ReadLine());
23             //        if (age >= 0 && age <= 100)
24             //        {
25             //            sum += age;
26             //        }
27             //        else
28             //        {
29             //            Console.WriteLine("输入的年龄不在正确范围内,程序退出!!!");
30             //            b = false;
31             //            break;
32             //        }
33             //    }
34             //    catch
35             //    {
36             //        Console.WriteLine("输入的年龄不正确,程序退出!!!");
37             //        b = false;
38             //        break;
39             //    }
40             //}
41             //if (b)
42             //{
43             //    Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
44             //}
45             //Console.ReadKey();
46 
47 
48        //     练习2:在while中用break实现要求用户一直输入用户名和密码,
49             //只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
50             //string name = "";
51             //string pwd = "";
52             //while (true)
53             //{
54             //    Console.WriteLine("请输入用户名");
55             //    name = Console.ReadLine();
56             //    Console.WriteLine("请输入密码");
57             //    pwd = Console.ReadLine();
58 
59             //    if (name == "admin" && pwd == "888888")
60             //    {
61             //        Console.WriteLine("登陆成功");
62             //        break;
63             //    }
64             //    else
65             //    {
66             //        Console.WriteLine("用户名或密码错误,请重新输入");
67             //    }
68             //}
69             //Console.ReadKey();
70 
71 
72             //1~100之间的整数相加,得到累加值大于20的当前数
73             //(比如:1+2+3+4+5+6=21)结果6 sum>=20  i
74             int sum = 0;
75             for (int i = 1; i <= 100; i++)
76             {
77                 sum += i;
78                 if (sum >= 20)
79                 {
80                     Console.WriteLine("加到{0}的时候,总和大于了20",i);
81                     break;
82                 }
83             }
84             Console.ReadKey();
85 
86 
87         }
88     }
89 }

三元表达式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _12三元表达式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             ////计算两个数字的大小 求出最大的
14             //Console.WriteLine("请输入第一个数字");
15             //int n1 = Convert.ToInt32(Console.ReadLine());
16             //Console.WriteLine("请输入第二个数字");
17             //int n2 = Convert.ToInt32(Console.ReadLine());
18             ////            语法:
19             ////表达式1?表达式2 :表达式3
20             //int max = n1 > n2 ? n1 : n2;
21             //Console.WriteLine(max);
22             ////if (n1 > n2)
23             ////{
24             ////    Console.WriteLine(n1);
25             ////}
26             ////else
27             ////{
28             ////    Console.WriteLine(n2);
29             ////}
30             //Console.ReadKey();
31 
32 
33             //提示用户输入一个姓名 只要输入的不是老赵  就全是流氓
34             Console.WriteLine("请输入姓名");
35             string name = Console.ReadLine();
36 
37             string result = name == "老赵" ? "淫才呀" : "流氓呀";
38             Console.WriteLine(result);
39             Console.ReadKey();
40 
41             //if (name == "老赵")
42             //{
43             //    Console.WriteLine("淫才呀");
44             //}
45             //else
46             //{
47             //    Console.WriteLine("流氓呀");
48             //}
49             Console.ReadKey();
50 
51 
52         }
53     }
54 }

随机数

 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             //产生随机数
14             //1、创建能够产生随机数的对象
15             //Random r = new Random();
16             //while (true)
17             //{
18 
19             //    //2、让产生随机数的这个对象调用方法来产生随机数
20             //    int rNumber = r.Next(1, 11);
21             //    Console.WriteLine(rNumber);
22             //    Console.ReadKey();
23             //}
24 
25             //输入名字随机显示这个人上辈是什么样的人
26             //思路:
27             //1、创建能够产生随机数的对象
28             //2、产生随机数 (1,7)
29 
30             Random r = new Random();
31             while (true)
32             {
33                 int rNumber = r.Next(1, 7);
34                 Console.WriteLine("请输入一个姓名");
35                 string name = Console.ReadLine();
36                 switch (rNumber)
37                 {
38                     case 1: Console.WriteLine("{0}上辈子是吃翔的", name);
39                         break;
40                     case 2: Console.WriteLine("{0}上辈子是拉翔的", name);
41                         break;
42                     case 3: Console.WriteLine("{0}上辈子就是一坨翔", name);
43                         break;
44                     case 4: Console.WriteLine("{0}上辈子是大汉奸", name);
45                         break;
46                     case 5: Console.WriteLine("{0}上辈子是拉皮条的", name);
47                         break;
48                     case 6: Console.WriteLine("{0}上辈子是救苦救难的活菩萨", name);
49                         break;
50                 }
51                 Console.ReadKey();
52             }
53 
54         }
55     }
56 }