新手
自己写了奇阶幻方,现在碰到问题是怎么把生成的幻方格式不变的储存到txt中,高手帮下忙,谢谢了
代码如下using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;namespace 奇阶幻方
{
    class Program
    {
        static int[,] hf;        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("奇阶幻方");
                Console.WriteLine("1.生成幻方");
                Console.WriteLine("2.清屏");
                Console.WriteLine("3.推出");
                Console.WriteLine("请选择[1,2,3];");                string cmd = Console.ReadLine();                switch (cmd)
                {
                    default:
                        Console.WriteLine();
                        break;
                    case "1":
                        生成幻方(输入阶数());
                        输出幻方();
                        break;
                    case "2":
                        Console.Clear();
                        break;
                    case "3":
                        return;
                }        static int 输入阶数()
        {
            int result = 0;
            while (true)
            {
                Console.Write("请输入一个正奇数");
                string input = Console.ReadLine();
                if (Regex.IsMatch(input, @"^[13579]$|^[1-9]\d{0,6}[13579]$"))
                {
                    result = Convert.ToInt32(input);
                    break;
                }
            }
            return result;
        }        static void 生成幻方(int js)
        {
            hf = new int[js, js];
            int row = 0;
            int col = js/2;
            hf[row, col] = 1;
            for (int i = 2; i <= js * js; i++)
            {
                if (row == 0 && col == js - 1)
                    row++;
                else if (col == js - 1)
                {
                    row--;
                    col = 0;
                }
                else if (row == 0)
                {
                    row = js - 1;
                    col++;
                }
                else if (hf[row - 1, col + 1] != 0)
                    row++;
                else if (hf[row - 1, col + 1] == 0)
                {
                    row--;
                    col++;
                }
                hf[row, col] = i;
            }
        }        static void 输出幻方()
        {
            int js = hf.GetLength(0);
            int width = hf.Length.ToString().Length + 1;
            string format = "{0," + width.ToString() + "}";
            width = Math.Max(Console.BufferWidth, width * js + 1);
            width = Math.Min(width, Int16.MaxValue);
            Console.BufferWidth = width;
            for (int i = 0; i < js; i++)
            {
                for (int j = 0; j < js; j++)
                {
                    Console.Write(format, hf[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}

解决方案 »

  1.   

    using System.IO;using(StreamWriter sw = File.AppendText(@"c:\123.txt)) 

        int m_Len=hf.Length;
        for(int i=0;i<m_Len;i++)
        sw.writeLine(hf[i].ToString(); 
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO; 
    namespace 奇阶幻方
    {
        class Program
        {
            static int[,] hf;
            static void Main(string[] args)
            {
                while (true) 
                { 
                    Console.WriteLine("奇阶幻方"); 
                    Console.WriteLine("1.生成幻方"); 
                    Console.WriteLine("2.清屏"); 
                    Console.WriteLine("3.推出"); 
                    Console.WriteLine("请选择[1,2,3];");                 string cmd = Console.ReadLine();                 switch (cmd) 
                    { 
                        default: 
                            Console.WriteLine(); 
                            break; 
                        case "1": 
                            生成幻方(输入阶数()); 
                            输出幻方(); 
                            break; 
                        case "2": 
                            Console.Clear(); 
                            break; 
                        case "3": 
                            return; 
                    } 
                }
            }        static int 输入阶数() 
            { 
                int result = 0; 
                while (true) 
                { 
                    Console.Write("请输入一个正奇数"); 
                    string input = Console.ReadLine(); 
                    if (Regex.IsMatch(input, @"^[13579]$|^[1-9]\d{0,6}[13579]$")) 
                    { 
                        result = Convert.ToInt32(input); 
                        break; 
                    } 
                } 
                return result; 
            }        static void 生成幻方(int js)
            {
                hf = new int[js, js];
                int row = 0;
                int col = js / 2;
                hf[row, col] = 1;
                for (int i = 2; i <= js * js; i++)
                {
                    if (row == 0 && col == js - 1)
                        row++;
                    else if (col == js - 1)
                    {
                        row--;
                        col = 0;
                    }
                    else if (row == 0)
                    {
                        row = js - 1;
                        col++;
                    }
                    else if (hf[row - 1, col + 1] != 0)
                        row++;
                    else if (hf[row - 1, col + 1] == 0)
                    {
                        row--;
                        col++;
                    }
                    hf[row, col] = i;
                }
            }        static void 输出幻方()
            {
                int js = hf.GetLength(0);
                int width = hf.Length.ToString().Length + 1;
                string format = "{0," + width.ToString() + "}";
                width = Math.Max(Console.BufferWidth, width * js + 1);
                width = Math.Min(width, Int16.MaxValue);
                Console.BufferWidth = width;
                for (int i = 0; i < js; i++)
                {
                    for (int j = 0; j < js; j++)
                    {
                        Console.Write(format, hf[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();            string mypath=@"C:\my.txt";
                FileStream fs = new FileStream(mypath, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);            for (int i = 0; i < js; i++)
                {
                    for (int j = 0; j < js-1; j++)
                    {
                        sw.Write(hf[i,j].ToString()+",");
                    }
                    sw.Write(hf[i, js-1].ToString());
                    sw.WriteLine();
                }
                sw.WriteLine();  
                sw.Close();
                fs.Close();
               
            }     }
    }