码迷,mamicode.com
首页 > Windows程序 > 详细

C# for循环

时间:2014-12-12 16:14:21      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   sp   for   strong   on   文件   

for(初始条件;比较;状态改变  )

循环体

//打印100遍helloword

for(int i=0;i<100,i++)

{

console.writeline("helloword");

}

 

////求1-100的和
            int s = 0;
            for(int i=1;i<=100;i++)
            {
                s = s + i;
            }
            Console.WriteLine(s);
            Console.ReadLine();

 

//100以内所有与7相关的数和,奇数和,偶数和
            int s = 0;
            for (int i = 1; i <= 100; i++)
            {
                if ((i % 7 == 0) || (i % 10 == 7) || (i / 10 == 7))
                {
                    s = s + i;
                }
            }
            Console.WriteLine(s);
            Console.ReadLine();

 

//100以内的所有质数和

    int sum = 0;
            for (int i = 2; i <=100; i++)
            {
                int a = 0;
                for (int j = 1; j <=i; j++)
                {
                    if (i % j == 0)//如果i能被它本身和1以外的数整除,那么他就不是质数
                    {
                       a++;
                    }
                }
                if (a == 2)  //能被整除的数只有两个,即为质数
                {
                    sum = sum + i;
                    Console.Write(i + "\t");
                }
            }
            Console.Write("和"+sum);
            Console.ReadLine();

 

 //打印☆,排列成图形

//右上
            for (int m = 1; m < 10; m++)
            {
                for (int n = 1; n <= m; n++)
                {
                    Console.Write("☆");
                }
                Console.Write("\n");
            }

 ////右下
            for (int m = 10; m > 1; m--)
            {
                for (int n = 1; n < m; n++)
                {
                    Console.Write("☆");
                }
                Console.Write("\n");
            }

////左下
            for (int m = 1; m < 10; m++)
            {
                for (int n = 1; n < m; n++)
                {
                    Console.Write("  ");
                }
                for (int l = 10; l > m; l--)
                {
                   Console.Write("☆");
                }
                Console.Write("\n");
            }

 ////左上
            for (int m = 1; m < 10; m++)
            {
                for (int n = 9; n >m; n--)
                {
                    Console.Write("  ");
                }
                for (int l = 1; l <= m;l++ )
                {
                   Console.Write("☆");
                }
            Console.Write("\n");
            }   

//上半部分
            for (int m = 1; m < 10; m++)
            {
                for (int n = 9; n > m; n--)
                {
                    Console.Write("  ");
                }
                for (int l = 1; l <= (2 * m - 1); l++)
                {
                    Console.Write("☆");
                }
                Console.Write("\n");
            }
 ////下半部分
            for (int m = 1; m < 9; m++)
            {
                for (int l = 1; l <= m; l++)
                {
                    Console.Write("  ");
                }
                for (int n = 1; n <= (2 * (9 - m) - 1); n++)
                {
                    Console.Write("☆");
                }
                Console.Write("\n");
            }
 
//一对幼兔一个月后长成小兔,再过一个月长成成兔并生下一对小兔,两年后有多少对兔子?
关系如下图
bubuko.com,布布扣
 
int a = 1, b = 0, c = 0;
            for (int i = 2; i <= 24; i++)
            {
                if (i == 1)
                {
                    c = 0;
                    b = 0;
                    a = 1;
                }
                else
                {
                    c = c + b;
                    b = a;
                    a = c;
                }
                Console.WriteLine("第{0}个月,成兔数量:{1}小兔数量{2}:幼兔数量{3}", i, c, b, a);
                Console.ReadLine();
            }
            Console.WriteLine("总数量为" + (c + a + b));
            Console.ReadLine();
 
//穷举问题:铅笔一支2元,笔盒5元,文件夹10元,彩笔5元,买正好100块的文具,每种至少买一种,列举出所有方法
for (int qianbi = 1; qianbi <= 50; qianbi++)
            {
                for (int he = 1; he <= 20; he++)
                {
                    for (int wenjianjia = 1; wenjianjia <= 10; wenjianjia++)
                    {
                        for (int caibi = 1; caibi <= 7; caibi++)
                        {
                            int a = qianbi * 2 + he * 5 + wenjianjia * 10 + caibi * 15;
                            if (a == 100)
                            {
                                Console.WriteLine("铅笔买{0}只,盒买{1}个,文件夹买{2}个,彩笔买{3}支", qianbi, he, wenjianjia, caibi);
                            }
                        }
                    }
                }
               
            }
            Console.ReadLine();

C# for循环

标签:style   blog   http   color   sp   for   strong   on   文件   

原文地址:http://www.cnblogs.com/jintuo/p/4159783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!