任何人都可以解决C#程序 [英] any one solve c# program

查看:79
本文介绍了任何人都可以解决C#程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后(四个)循环中有多少值变量j?为什么?


how much value variable j in last(four) for loop ? why??


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

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                int j;
                for (int k = 1; k <= n - i; k++)
                {
                    Console.Write(" ");
                }
                for (j = 1; j <= i; j++)
                {
                    Console.Write("" + j);
                }
                for (int s = j - 2; s >= 1; s--)
                {
                    Console.Write("" + s);
                }


                Console.WriteLine("");
            }


            Console.ReadLine();
        }
    }
}

推荐答案

使用调试器是理解程序的最简单方法.您只需要逐步执行它并检查变量即可.

无论如何,对于像这样的简单程序,如果执行一次,那么每行的作用就应该很明显.

而且仅通过查看代码就可以很容易地弄清楚该程序的功能. Last循环仅从最后打印的数字之前的数字开始向后打印数字.实际上,在上一个循环之后,j比上次打印的值大1,然后从该数字中减去2得出最后一次打印的值之前的数字.

尝试在数量较少的纸张上进行尝试(例如,输入3).​​
Using a debugger it the easiest way to understand the program. You just have to execute it step by step and inspect the variables.

Anyway for a simple program like this one, if you execute it once, it should then become obvious what each line does.

And it is pretty easy to figure out what this program do only by looking at the code. Last loop simply print number backward starting with the number just before last printed number. In fact after the previous loop, j is 1 more than the last printed value and substracting 2 from that number give the number just before the last printed value.

Try it on paper with a small number (for exemple, an input of 3).


将其放入Visual Studio编辑器中并进行调试.或者如果您有此代码的"C"版本,则也可以在那里调试.通过按F8
put it in Visual studio Editor and debug it. or if you have a ''C'' version of this code then you can debug overthere too. by pressing F8


这篇关于任何人都可以解决C#程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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