Magic Sqaure源代码.有人可以解释吗? :) [英] Magic Sqaure source code. anyone can explain? :)

查看:61
本文介绍了Magic Sqaure源代码.有人可以解释吗? :)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,谁能解释一下此源代码行中发生的情况?不太了解
使用系统;

Hey can anyone please explain this source code line for line whats happen? Dont fully understand
using System;

class Program
{
    static void Main(string[] args)
    {
	bool correctInput = false;
	int n = 0;
	while (!correctInput) {
        	Console.WriteLine("Input an odd number:");
	        string input = Console.ReadLine();
		if (!int.TryParse(input, out n))
		{		
		Console.WriteLine("This is not a number ''" + input + "''!\nPress any key to retry");
		Console.ReadLine();
	        }
		else if (Convert.ToInt16(input) % 2 == 0)
		{
		Console.WriteLine("This is not an odd number ''" + input +"''!\nPress any key to retry");
		Console.ReadLine();
	        }
	        else
		{
			n = Convert.ToInt16(input);
			correctInput = true;
		}
	}
        int nSquare = n * n;
        int y = 0;
        int x = (n / 2);
        int[,] magicSquare = new int[n, n];
        for (int a = 1; a <= nSquare; ++a)
        {
            magicSquare[y, x] = a;
            y--;
            x++;
            if (a % n == 0)
            {   
                y += 2;
                x -= 1;
            }
            else
            {
                x = (x + n) % n;
                y = (y + n) % n;
            }
        }
        for (int a = 0; a < n; a++)
        {
            for (int b = 0; b < n; b++)
            {
 Console.Write(Convert.ToString(magicSquare[a, b]).PadLeft((Convert.ToString(nSquare).Length), '' '') + " ");
            }
            Console.WriteLine("");
        }
    }
}


谢谢您:)


Thank you :)

推荐答案

您是否知道在逐行解释代码方面涉及多少工作?

每行代码将占用一段文本!
例如:
Do you have any idea how much work is involved in explaining code to someone "line-by-line"?

Each line of code will take about a paragraph of text!
For example:
if (a % n == 0)

取变量"a"中保存的值,通过将其除以变量"n"中保存的值并计算出余数来计算其模量.如果该值为零,则执行以下语句块.如果不是,请查看下一个语句块后面是否有"else",然后执行紧随其后的语句块.

这是由10个重要字符组成的简单行!

否.没人会逐行向您解释整个程序.

如果您有特定的问题,请询问该问题.如果没有,那就坐下来喝杯咖啡,逐步完成程序!您可能会发现调试器会有所帮助...

Take the value held in the variable "a" and calculate the modulus of it by dividing it by the value held in the variable "n" and working out the remainder. If this value is zero, then execute the following statement block. If it isn''t, then look to see if there is an "else" following the next statement block, and execute the statement block following that instead.

And this is a simple line of 10 significant characters!

No. Nobody is going to explain the entire program to you line by line.

If you have a specific problem, then ask about it. If not, then sit down with a cup of coffee and work your way through the program! You may find the debugger helps...


最好仅提供一个参考文献即可回答该问题.

每个人都需要阅读以下内容:
Peter Norvig,十年自学编程
http://norvig.com/21-days.html [
The question is best answered by just one reference.

Everyone needs to read this, first of all:
Peter Norvig, Teach Yourself Programming in Ten Years,
http://norvig.com/21-days.html[^].

If you accept it as a working plan, those ten years will be much better spent compared to the few minutes you wasted for your question!

Cheers,
--SA


您不了解其中的什么?是代码吗?还是逻辑?不过,我在代码中看不到任何复杂的东西:)就逻辑而言,它是某种算法.我认为没有人会为您写下算法:)

所以我想说,在这行int nSquare = n * n;上放置一个断点,然后运行您的程序,输入一个奇数,例如7或11,并逐步执行每个流程.写下逻辑和执行路径,花几分钟时间进行阅读和思考,您会明白的.

另外,请查看以下内容:)
http://everything2.com/title/algorithm+for + generating + odd-order + magic + squares [ ^ ]
What do you don''t understand in it? Is it the code? or is it the logic? I do not see anything complex in the code though :) As far as for the logic, it''s some algorithm. I do not think anyone would write down the algorithm out for you :)

So I''d say, place a break point on this line int nSquare = n * n; and then run your program, enter a odd number like 7 or 11 and step through each flow. Write the logic down and execution paths, give it few minutes of reading and thought, you''ll get it.

Also, look into this :) http://everything2.com/title/algorithm+for+generating+odd-order+magic+squares[^]


这篇关于Magic Sqaure源代码.有人可以解释吗? :)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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