Visual Studio中的简单嵌套循环程序 [英] Simple nested loop program in Visual Studio

查看:203
本文介绍了Visual Studio中的简单嵌套循环程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定您已经解决了这个问题很多年了,但是作为一个新的程序员,当我尝试在Visual Studio中编译一个简单的嵌套循环程序时,我得到了相同的代码(我忘了添加最里面的while循环中的一个num ++(一旦我执行了WriteLine 最内层循环中的命令...显示0 0 0 0 0(永远重复).修复它后,它仍然会打印12 16(我以为它会打印12 13 14 15 16(我仍然不知道为什么不这样.)但是也许我没有经验的结果可能会给您一些帮助. 洞悉您现实生活中发生的事情.我认为许多编程的规则"像单向单出"那样被破坏休息一下...而且我认为嵌套循环会做一些有问题的事情,我将尝试避免 它们的用法.代码是:

I'm sure you've been done with this problem for years, but as a new programmer, I got that same code when I tried to compile a simple nested loop program in Visual Studio (I forgot to add a num++ in the innermost while loop (once I did a WriteLine command in the innermost loop...it showed 0 0 0 0 0 (repeating forever).  Once I fixed it, It still printed 12 16 (I thought it would print 12 13 14 15 16 (I still don't know why it doesn't.  But maybe my inexperienced result might give you some insight as to what happened in your real life situation.  I think a lot of programming "rules" are being broken like "one way in one way out" with the breaks...and also I think nested loops do loopy things, I'm going to try to avoid their usage.  The code is:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication4
{
   课程计划
    {
      静态void Main(string [] args)
       {
           int num = 0;
          而(小于20)
           {
                            如果(num == 17)
                   休息;
                            一会儿(num< 16)
                             {
                   如果(num == 12)
               b休息;
                              
                    num ++;
                             }
                             Console.WriteLine(num);
                             num ++;
           }
       }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 0;
            while (num < 20)
            {
                if (num == 17)
                    break;
                while (num < 16)
                {
                    if (num == 12)
                        break;
                 
                    num++;
                }
                Console.WriteLine(num);
                num++;
            }
        }
    }
}

推荐答案

看看你的逻辑.它将仅打印12和16,是因为......

Looking at your logic. It is going to print 12 and 16 only because....

第一次循环浏览嵌套while(num< 16)时,它将在12且num = 12时退出循环

The first time you loop through your nested while (num < 16) it will exit the loop at 12 and num = 12

然后打印12,并将num递增到13

it then prints 12 and increments num to 13

然后它返回外循环,而(num< 20)num仍小于16,并且由于13小于16而将再次落入内循环...

it then returns to the outer loop while (num < 20) num is still less than 16 and will fall into the inner loop again because 13 is less than 16... 

然后它再次增加到16,然后退出.

then it increments again to 16 and steps out.

然后打印16并递增到17

it then prints 16 and increments to 17

然后在17处突破外层并结束.

then breaks out of the outer at 17 and ends.


这篇关于Visual Studio中的简单嵌套循环程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆