for语句嵌套

//分支语句可以嵌套
            //循环语句,同样可以嵌套

例一:     //打印三角形:左下角是直角            

Console.Write("请输入一个正整数:");            

 int a = int.Parse(Console.ReadLine());            

for (int i = 1; i <= a; i++)            

{            

    for (int k = 1; k <= i; k++)            

    {            

        Console.Write("☆");            

    }            

     Console.WriteLine();            

}

Console.ReadLine();

例二:打印三角形,左上角是直角            

Console.Write("请输入一个正整数:");            

int a = int.Parse(Console.ReadLine());            

for (int i = a; i >0; i--)            

{                

for (int k = i; k >0; k--)                

{                    

Console.Write("☆");                

}                

Console.WriteLine();            

}

            Console.ReadLine();

例三://输入一个正整数,求1!+2!+...+n!            

Console.Write("请输入一个正整数:");            

int a = int.Parse(Console.ReadLine());

int sum = 0;//为了接收所有阶乘的和           

for (int i = 1; i <= a; i++)            

{                

int jie = 1;//写在这个位置是为了,每次外循环走到这里,都让之前的阶乘的结果变成1                

for (int j = 1; j <= i; j++)                

{//从1开始算每个数的阶乘                    

jie *= j;                

}                

sum += jie;//每个数的阶乘算出来之后,加起来            

}          

例四:  //99乘法口诀表
            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(j + "*" + i + "=" + (j * i));
                    Console.Write("\t");
                }
                Console.WriteLine();
            }  

例五:    //有一张超大的纸
            //纸张的厚度是0.07mm
            //对折多少次,可以到达珠峰的高度,
            //按照8848m来计算
            int height = 7;
            for(int i =1;i>0;i++)
            {
               height *=2;
               if (height >= 884800000)
              {
                   Console.WriteLine("总共需要对折至少"+i+"次,才能够到达珠峰的高度");
                   break;
               }
            }

Console.WriteLine("结果是:" + sum);

例六:  //一个月的时间,30天
            //第一天,1分钱
            //第二天是第一天的两倍
            //以此类推
            //算一下这一个月能赚多少钱
            double sum = 0;
            for (int i = 1; i <= 30; i++)
            {
                double a = 0.01;
                for (int j = 1; j <= i; j++)
                {
                    a *= 2;
                }
                sum += a;
            }
            Console.WriteLine("共计赚取了" + sum + "元。");

 

你可能感兴趣的:(for语句嵌套)