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的结果类型一致,并且也要跟整个三元表达式的结果类型一致
01复习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01复习
{
class Program
{
static void Main(string[] args)
{
//练习4:不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出。
//循环体:提示用户输入一个数字 接收 转换 打印2倍
//循环条件:输入的不能是q
//string input = "";
//while (input != "q")
//{
// Console.WriteLine("请输入一个数字,我们将打印这个数字的2倍");
// //不能直接转换成int类型 因为用户有可能输入q
// input = Console.ReadLine();//数字 q 乱七八糟
// if (input != "q")
// {
// try
// {
// int number = Convert.ToInt32(input);
// Console.WriteLine("您输入的数字的2倍是{0}", number * 2);
// }
// catch
// {
// Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
// }
// }
// else//==q
// {
// Console.WriteLine("输入的是q,程序退出");
// }
//}
//练习5:不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候显示刚才输入的数字中的最大值
//循环体:提示用户输入一个数字 接收 转换成int类型 不停的比较大小
//循环条件:输入的不能是end
//F11
string input = "";
int max = 0;
while (input != "end")
{
Console.WriteLine("请输入一个数字,输入end我们将显示你输入的最大值");
input = Console.ReadLine();//数字 end 乱七八糟
if (input != "end")
{
try
{
int number = Convert.ToInt32(input);
//让用户输入的每个数字都跟我假定的最大值比较,只要比我假定的最大值要大,
//就把当前输入的这个数字赋值给我的最大值
if (number > max)
{
max = number;
}
}
catch
{
Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
}
}
else//==end
{
Console.WriteLine("您刚才输的数字中的最大值是{0}",max);
}
}
Console.ReadKey();
Console.ReadKey();
}
}
}
02调试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02调试
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
//程序运行断点处,就不在向下执行了
Console.WriteLine("这行代码有可能有错误");
Console.WriteLine("这行代码有可能有错误");
Console.WriteLine("这行代码有可能有错误");
Console.WriteLine("这行代码有可能有错误");
Console.WriteLine("这行代码有可能有错误");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
}
}
}
03for循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03for循环
{
class Program
{
static void Main(string[] args)
{
//向控制台打印10遍 欢迎来到传智播客.Net学习
for (int i = 0; i < 10; i++)
{
Console.WriteLine("欢迎来到传智播客.Net学习{0}", i);
}
//for (int i = 0; i < length; i++)
//{
//}
Console.ReadKey();
//int i = 0;//定义循环的次数
//while (i < 10)
//{
// Console.WriteLine("欢迎来到传智播客.Net学习");
// i++;
//}
//Console.ReadKey();
}
}
}
04for循环的正序输出和倒序输出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04for循环的正序输出和倒序输出
{
class Program
{
static void Main(string[] args)
{
//请打印 1-10
int i = 0;
for (; i < 10; )
{
Console.WriteLine("欢迎来到传智播客.Net学习");
i++;
}
Console.ReadKey();
//for (int i = 1; i <= 10; i++)
//{
// Console.WriteLine(i);
//}
////打印10-1
//for (int i = 10; i >= 1; i--)
//{
// Console.WriteLine(i);
//}
//for (int i = 10; i >= 1; i--)
//{
// Console.WriteLine(i);
//}
Console.ReadKey();
}
}
}
05for循环的练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05for循环的练习
{
class Program
{
static void Main(string[] args)
{
//求1-100之间的所有整数和 偶数和 奇数和
//int sum = 0;
//int n = 100;
//for (int i = 1; i <= n; i += 2)
//{
// sum += i;
//}
//Console.WriteLine(sum);
//Console.ReadKey();
//找出100-999间的水仙花数?
//水仙花数指的就是 这个百位数字的
//百位的立方+十位的立方+个位的立方==当前这个百位数字
//153 1 125 27 153 i
//百位:153/100
//十位:153%100/10
//个位:153%10
for (int i = 100; i <= 999; i++)
{
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
{
Console.WriteLine("水仙花数有{0}",i);
}
}
Console.ReadKey();
}
}
}
06for循环的嵌套
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _06for循环的嵌套
{
class Program
{
static void Main(string[] args)
{
//当遇到某个事情要做一遍,而另外一个事情要做N遍的时候
//for循环的嵌套
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("Hello World i循环了{0}次,j循环了{1}次",i,j);
break;
}
}
Console.ReadKey();
}
}
}
07乘法口诀表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07乘法口诀表
{
class Program
{
static void Main(string[] args)
{
//for (int i = 1; i <= 9; i++)
//{
// for (int j = 1; j <= i; j++)
// {
// Console.Write("{0}*{1}={2}\t", i, j, i * j);
// }
// Console.WriteLine();//换行
//}
//Console.ReadKey();
//Console.Write("Hello Wor\tld");
//Console.WriteLine();
//Console.Write("Hello World");
//Console.ReadKey();
Console.WriteLine("请输入一个数字");
int number = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <=number; i++)
{
Console.WriteLine("{0}+{1}={2}",i,number-i,number);
}
Console.ReadKey();
}
}
}
08类型转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08类型转换
{
class Program
{
static void Main(string[] args)
{
//使用Convert进行转换 成功了就成了, 失败了就抛异常
// int numberOne = Convert.ToInt32("123abc");
// int number = int.Parse("123abc");
//Console.WriteLine(number);
int number = 100;
//参数 返回值
bool b = int.TryParse("123abc", out number);
Console.WriteLine(b);
Console.WriteLine(number);
//方法 或者 函数?
Console.ReadKey();
}
}
}
09for循环的3个练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09for循环的3个练习
{
class Program
{
static void Main(string[] args)
{
//练习1:循环录入5个人的年龄并计算平均年龄,
//如果录入的数据出现负数或大于100的数,立即停止输入并报错.
//int sum = 0;
//bool b = true;
//for (int i = 0; i < 5; i++)
//{
// Console.WriteLine("请输入第{0}个人的成绩",i+1);
// try
// {
// int age = Convert.ToInt32(Console.ReadLine());
// if (age >= 0 && age <= 100)
// {
// sum += age;
// }
// else
// {
// Console.WriteLine("输入的年龄不在正确范围内,程序退出!!!");
// b = false;
// break;
// }
// }
// catch
// {
// Console.WriteLine("输入的年龄不正确,程序退出!!!");
// b = false;
// break;
// }
//}
//if (b)
//{
// Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
//}
//Console.ReadKey();
// 练习2:在while中用break实现要求用户一直输入用户名和密码,
//只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
//string name = "";
//string pwd = "";
//while (true)
//{
// Console.WriteLine("请输入用户名");
// name = Console.ReadLine();
// Console.WriteLine("请输入密码");
// pwd = Console.ReadLine();
// if (name == "admin" && pwd == "888888")
// {
// Console.WriteLine("登陆成功");
// break;
// }
// else
// {
// Console.WriteLine("用户名或密码错误,请重新输入");
// }
//}
//Console.ReadKey();
//1~100之间的整数相加,得到累加值大于20的当前数
//(比如:1+2+3+4+5+6=21)结果6 sum>=20 i
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
if (sum >= 20)
{
Console.WriteLine("加到{0}的时候,总和大于了20",i);
break;
}
}
Console.ReadKey();
}
}
}
10continue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10continue
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Hello World");
// break;
continue;
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
}
Console.ReadKey();
}
}
}
11continue练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _11continue练习
{
class Program
{
static void Main(string[] args)
{
//练习1:用 while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和。
//int sum = 0;
//int i=1;
//while (i <= 100)
//{
// if (i % 7 == 0)
// {
// i++;
// continue;
// }
// sum += i;
// i++;
//}
//Console.WriteLine(sum);
//Console.ReadKey();
//找出100内所有的素数
//素数/质数:只能被1和这个数字本身整除的数字
//2 3 4 5 6 7
//7 7%1 7%2 7%3 7%4 7%5 7%6 7%7 6%2
for (int i = 2; i <= 100; i++)
{
bool b = true;
for (int j = 2; j <i; j++)
{
//除尽了说明不是质数 也就没有再往下继续取余的必要了
if (i % j == 0)
{
b = false;
break;
}
}
if (b)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
//6 6%2 6%3
}
}
}
12三元表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _12三元表达式
{
class Program
{
static void Main(string[] args)
{
////计算两个数字的大小 求出最大的
//Console.WriteLine("请输入第一个数字");
//int n1 = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("请输入第二个数字");
//int n2 = Convert.ToInt32(Console.ReadLine());
//// 语法:
////表达式1?表达式2 :表达式3
//int max = n1 > n2 ? n1 : n2;
//Console.WriteLine(max);
////if (n1 > n2)
////{
//// Console.WriteLine(n1);
////}
////else
////{
//// Console.WriteLine(n2);
////}
//Console.ReadKey();
//提示用户输入一个姓名 只要输入的不是老赵 就全是流氓
Console.WriteLine("请输入姓名");
string name = Console.ReadLine();
string result = name == "老赵" ? "淫才呀" : "流氓呀";
Console.WriteLine(result);
Console.ReadKey();
//if (name == "老赵")
//{
// Console.WriteLine("淫才呀");
//}
//else
//{
// Console.WriteLine("流氓呀");
//}
Console.ReadKey();
}
}
}
13、随机数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _13_随机数
{
class Program
{
static void Main(string[] args)
{
//产生随机数
//1、创建能够产生随机数的对象
//Random r = new Random();
//while (true)
//{
// //2、让产生随机数的这个对象调用方法来产生随机数
// int rNumber = r.Next(1, 11);
// Console.WriteLine(rNumber);
// Console.ReadKey();
//}
//输入名字随机显示这个人上辈是什么样的人
//思路:
//1、创建能够产生随机数的对象
//2、产生随机数 (1,7)
Random r = new Random();
while (true)
{
int rNumber = r.Next(1, 7);
Console.WriteLine("请输入一个姓名");
string name = Console.ReadLine();
switch (rNumber)
{
case 1: Console.WriteLine("{0}上辈子是吃翔的", name);
break;
case 2: Console.WriteLine("{0}上辈子是拉翔的", name);
break;
case 3: Console.WriteLine("{0}上辈子就是一坨翔", name);
break;
case 4: Console.WriteLine("{0}上辈子是大汉奸", name);
break;
case 5: Console.WriteLine("{0}上辈子是拉皮条的", name);
break;
case 6: Console.WriteLine("{0}上辈子是救苦救难的活菩萨", name);
break;
}
Console.ReadKey();
}
}
}
}