新手
自己写了奇阶幻方,现在碰到问题是怎么把生成的幻方格式不变的储存到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();
}
}
}
自己写了奇阶幻方,现在碰到问题是怎么把生成的幻方格式不变的储存到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();
}
}
}
解决方案 »
- 问下C# 调用 C++ 接口 问题 有点难度 请进
- asp.net在C#环境下如何查询、添加、修改access数据库中的记录
- C#的winform中,datagridview中动态生成的textbox,如何获得textbox里面的值
- 在console中,出现多线程问题
- 在c#中怎么实现打开文件,得到文件指定的图形扩展名文件
- 如何将richtextbox中的内容转换为字节数组?急急!!
- TransactionScope锁表的问题
- 一个遭遇,望大侠执教
- 求一个直接转换的方法
- 怎样能从XmlDocument获得对应的stream?
- DataGridView前幾列數據如果有改動則改變后兩列的數據。
- 字母排序
{
int m_Len=hf.Length;
for(int i=0;i<m_Len;i++)
sw.writeLine(hf[i].ToString();
}
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();
} }
}