vr引擎设计-C#基础第三天

81 阅读2分钟

一、循环语句

1、while循环

1、基本结构

image.png

2、死循环

image.png

3、break 与 continue

image.png

1break;  跳出当前整个循环

2continue;  跳出当前这一次循环,继续执行下一次循环的内容

4、练习一

image.png

5、练习二

image.png

6、练习三

image.png

7、练习四

image.png 提示:

image.png

2.for循环

1、基本结构

image.png

2、练习

image.png

3、do...while循环

基本结构

image.png

4、关于循环的其它题目

1、练习一

image.png

2、练习二

image.png

3、练习三

image.png

5、变量作用域

1、同一个作用域中的变量不能重名

2、父作用域与子作用域,可以访问父作用域里面的变量,但是父作用域不能访问子作用域中定义的变量

6、循环嵌套

1、练习一

image.png

2、练习二

image.png

3、练习三

image.png

4、练习四

image.png

5、练习五

image.png

代码展示

using System;
using System.Reflection.Metadata.Ecma335;

namespace demo3
{
    class program
    {
        static void Main(string[] args)
        {
            /* string a = Console.ReadLine();
             switch (a)
             {
                 case "星期一":
                     Console.WriteLine("回来上课");
                     break;
                 case "星期二":
                     Console.WriteLine("上课");
                     break;
                 case "星期三":
                     Console.WriteLine("挺好的");
                     break;
                 case "星期四":
                     Console.WriteLine("太惨了");
                     break;
                 case "星期五":
                     Console.WriteLine("回来上课");
                     break;
                     default: Console.WriteLine("放假");
                     break;
             }*/


            /*while (循环的判断条件) 如果条件为真就进入循环体,直到为假,跳出循环
           {
                循环体
            }*/

            //死循环,条件一直为真
            /*while (3 > 2)
            {
                Console.WriteLine(1);
            }*/

            //控制循环次数,怎么办?
            //比如可以改变条件的值,让条件有一天为假
            /* int a = 1;
             while(a > -1)   //a减少才能变为假
             {
                 Console.WriteLine(a);// 结果1,0
                 a--;  //a最终的值为-1
             }

             int b = 1;
             while (b <= 10)
             {
                 Console.WriteLine(b);
                 b++;
             }*/

            /* int k = 0;
             while (true)
             {
                 Console.WriteLine(k);
                 if (k == 100) break;    //如果if语句后面只有一条语句的时候,大括号是可以省略的
                 k++;

                 //break跳出当前整个循环   continue跳出当前这一次循环,还会进入下一次循环
             }*/

            /*  //例子:输入整数n,输出1+2+3+...+n的结果
              int c = Convert.ToInt32(Console.ReadLine());
              int d = 1;
              int e = 0;
              while (d <= c)
              {
                  e += d;
                  d++;               
              }
              Console.WriteLine(e);

              //例子:输出1-100之间的所有偶数
              int f = 1;
              while (f <= 100)
              {
                  if (f % 2 == 0)
                  {
                      Console.WriteLine(f);
                  }
                  f++;
              }*/

            /*例子:对于任意大于1的自然数n,若n为奇数,将n编程3n+1,否则变成n的一般,经过若干次这样的变化,
            n一定会最终变成1,输出n,输出变换的次数*/
            /*int g = Convert.ToInt32(Console.ReadLine());
            int h = 0;
            while (g > 1)
            {
                if(g % 2 == 0)
                {
                    g=g / 2;
                }
                else
                {
                    g = 3 * g + 1;
                }
                h++;
            }
            Console.WriteLine(g);
            Console.WriteLine(h);*/

            /*//输入学生个数,再输入学生成绩,求平均年龄,保留两位小数
            int i =Convert.ToInt32(Console.ReadLine());
            int j = i;
            double sum = 0;
            while (i > 0)
            {
                int num =Convert.ToInt32(Console.ReadLine());
                sum += num;
                i--;

            }
            Console.WriteLine("平均年龄为"+Math.Round(sum/j,2));*/

            //for循环
            /*for (; ; ) //初始化变量;表达式判断条件;增量表达式  如果中间表达式没有写就默认为真
            {
                Console.WriteLine(1);
            }*/

            /*for(int i = 0; i < 10; i++)
            {
                Console.WriteLine(1);
            }*/

            /*//例子:输入两个正整数n和m,计算n和m之间所有能被17整除的数的和
            int n = Convert.ToInt32(Console.ReadLine());
            int m = Convert.ToInt32(Console.ReadLine());
            if (n > m)
            {
                int o =m;
                m = n;
                n = o;
            }
            int sum = 0;
            for(;n<=m; n++)
            {
                if (n % 17 == 0)
                {
                    sum += n;
                }               
            }
            Console.WriteLine(sum);*/

            /*//do...while循环 多执行一次 先执行再判断   while循环的区别
            int k = 0;
            do
            {
                Console.WriteLine(1);
                k++;
            }while (k < 10);*/

            /*输入一个整数a,和一个正整数n,计算乘方a的n次方
             * Console.WriteLine("请输入一个整数a:");
            int a =Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入一个正整数n:");
            int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 1; i < n; i++)
            {
                a *= a;
            }
            Console.WriteLine(a);*/

            /* //例子:球落地
             double q =Convert.ToInt32(Console.ReadLine());
             double sum = 0;
             for(int i = 0; i < 2; i++)
             {
                 q = q / 2;
                 sum = sum + (q*3);
             }
             Console.WriteLine(q);
             Console.WriteLine(sum);

             Console.ReadLine();*/

            /*//打印水仙花数  是指一个三位数,例如153=1*1*1+5*5*5+3*3*3
            int a;
            int b;
            int c;
            for (int i = 100; i < 1000; i++)
            {
                a = i / 100;
                b = i % 100 / 10;
                c = i % 10;
                if (a * a * a + b * b * b + c * c * c == i)
                {
                    Console.WriteLine(i);
                }
                
            }*/

            /*//例子:输入整数n,输出n层的三角形
            int t=Convert.ToInt32(Console.ReadLine());
            //两层循环 外层循环n层  里面循环再循环每层要输出的*
            for(int i = 1; i <= t; i++)
            {
                for(int j = 1; j <= i; j++)
                {
                    Console.Write("*"); //输出每一行中的*
                }
                Console.WriteLine();    //输出完一行后要进行换行的操作
            }*/

            /*//输入一个正整数n,输出n层的等腰三角形
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n - i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= (2 * i - 1); k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();

            }*/

            /*//菱形
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n - i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= (2 * i - 1); k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();

            }
            //循环出倒三角  总共有n-1层 
            for (int i = 1; i <= n - 1; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= (2 * (n - i) - 1); k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();

            }*/

            

            /*//倒序99乘法表
            int i, j;
            for (i = 9; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}={2} ", i, j, i * j);
                }
                Console.WriteLine();
            }*/


            //自定义价格 百文买鸡
            int xprice =Convert.ToInt32(Console.ReadLine());
            int yprice =Convert.ToInt32(Console.ReadLine());
            int zprice =Convert.ToInt32(Console.ReadLine());

            for(int x = 1; x <= 100 / xprice; x++)
            {
                for(int y = 1; y <= 100 / yprice; y++)
                {
                    for (int z = 1; z <= 100 / zprice; z++)
                    {
                        if(x * 3 + y * 5 + z * 2 == 100 && x + y + z <= 100)
                        {
                            Console.WriteLine("公鸡有{0}只,母鸡有{1}只,小鸡有{2}只", x, y, z);
                        }
                    }
                }
            }


        }
    }
}