【C#视频】——循环

顺序结构

if

Console.writeline("请输入跪键盘时间");
String strMin=console.readline();
Int min =console.ToInt32(strMin);
Bool result=min>60;
If(result)//条件或者是bool值
{
	Console.writeline("不用给键盘了");
}
Console.readkey();

if...else

Console.WriteLine("请输入年份");
String year=Convert.ToInt32(Console.ReadLine());
bool result=(year%400==0)||(year%4==0&&year%100!=0);
if (result)
{
	Console.WriteLine("闰年");
}
else
{
	Console.WriteLine("不是闰年");
}
Console.ReadKey();

if...else if...

            string str = "";
            Console.WriteLine("输入成绩");
            int score = Convert.ToInt32(Console.ReadLine());
            if (score>=90)
            {
                str="A";
            }
            else if(score>=80)
            {
                str="B";
            }
            else if(score>=70)
            {
                str="C";
            }
            else if(score>=60)
            {
                str="D";
            }
            else
            {
                str="E";
            }

            Console.WriteLine(str);
            Console.ReadKey();

switch-case

是多分支结构

switch (num)
{
    case 1: per[i] = new Person(); break;
    case 2: per[i] = new Teacher(); break;
    case 3: per[i] = new Student(); break;
    case 4: per[i] = new ShuaiGuo(); break;
    case 5: per[i] = new MeiLv(); break;
    case 6: per[i] = new YeShou(); break;
}

try-carch

异常处理

            try
            {
                int num = Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception ex)
            {
                throw;//专门扔出来异常
                Console.WriteLine(ex.Message);
            }
            //无论是否出异常都走这里
            finally
            {
                Console.WriteLine("杨洋好帅");
            }
            Console.ReadKey();

循环结构

while循环

先判断,然后再执行循环体

循环可以一次都不执行

            //2006年培养学员80000人,每年增长25%,请问按此增长速度,
            //到哪一年培训学员人数将达到20万人?

            double student = 80000;
            int year = 2006;
            while (student<=200000)
            {
                student *= 1.25;
                year++;
            }
            Console.WriteLine("{0}年是{1}人",year,student);
            Console.ReadKey();

do-while循环

先执行一次循环,然后再判断条件是否成立

循环至少会执行一次

String name ="";
String pwd="";
do
{
Console.WriteLine("请输入账号");
name=Console.ReadLine();
Console.WriteLine("请输入密码");
pwd=Console.ReadLine();
}while (name!="admin"||pwd!="8888");
Console.WriteLine("程序结束");
Console.ReadKey();

for循环

//求1-100间的所以偶数和
int sum=0;
for(int i=0; I <= 100; i++)
{
	if (i%2==0)
	{
	sum += I;
	}
}
Console.WrteLine("总和为{0}",sum);
Console.ReadKey();

foreach

提供了遍历数组的简单方式

foreach (array_expression as value)
       statement


跳转语句

break

用于循环中,退出当前循环

int i=0;
while(true)
{
    if(i==5)
    {
        Console.WriteLine("小羊很帅");
        break;
     }
     i++;
}
Console.WriteLine("系统识别此话是真的");
Console.ReadKey();

continue

int i=1;
int sum=0;
while (i <= 100)
{
     if (i % 7 == 0)
     {
         i++;
         continue;
     }
     sum += i;
     i++;           
}
Console.WriteLine("总和为{0}",sum);
Console.ReadKey();

猜你喜欢

转载自blog.csdn.net/mirabellezwh/article/details/80459147