C#--二维数组显示成矩阵

fenghy2008 2009-08-17 04:16:16
各位高手朋友你们好!
小弟现有一问题不知道如何解决,希望各位高手能多多帮忙,给点意见。问题描述如下:
一个集合为{"你","我","他","它","好","为","怀","请","学"},然后初始化string[3,3]的二维数组。要求每次初始化的结果不同,进而显示的矩阵结果也不同。例如:第一次初始化string[3,3]={"你","我","他","它","好","为","怀","请","学"} 从而显示的矩阵形式为:你 我 他
它 好 为
怀 请 学

而第二次初始化string[3,3]={"好","学","你","请","怀","我","它","为","他"}进而得到的矩阵为:好 学 你
请 怀 我
它 为 他

也就是说初始化二维数组不同,进而显示的矩阵也就不同。同时,当用户输入矩阵的坐标时,能够返回其坐标对应的内

容。例如:你 我 他 这个3*3的矩阵,当用户输入坐标(1,1)则返回对应的--"好"。
它 好 为
怀 请 学

请各位高手们多多帮忙,最好能提供思路和实现源码。万分感激!!
...全文
1583 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
asp460 2010-10-04
  • 打赏
  • 举报
回复
看起来很难!
cppfaq 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 fenghy2008 的回复:]
谢谢,那个已经实现了。
[/Quote]
那就结贴吧
fenghy2008 2009-08-19
  • 打赏
  • 举报
回复
谢谢,那个已经实现了。
风之影子 2009-08-19
  • 打赏
  • 举报
回复
您不是吧,这个你也不自己来实现吗?
fenghy2008 2009-08-18
  • 打赏
  • 举报
回复
libinguest你好!
你所提供的代码能实现通过richtextbox每次加载显示不同的矩阵结果。如果现在界面上有一个textbox文本框,用来接收用户输入的矩阵坐标值,然后返回矩阵中对应的字符串。这个过程如何实现呢?麻烦你帮帮忙,谢谢!!
fenghy2008 2009-08-18
  • 打赏
  • 举报
回复
cppfaq你好!
首先感谢你提供的实现源码,在控制台下运行后没问题。但是我最初的想法是通过界面来实现,即将每次加载显示的矩阵通过winform中的控件加载后在界面上显示,并且提供一个textbox文本框,接收用户输入的矩阵的坐标值,然后返回矩阵中对应的某个字符串。也就是说想把在控制台下实现的整个过程通过界面形式完成。所以能否麻烦高手再帮帮忙看看如何通过界面的形式实现?万分感谢!
yjj3844 2009-08-17
  • 打赏
  • 举报
回复
这样就可以了吗? 不会又会出错把~
风之影子 2009-08-17
  • 打赏
  • 举报
回复
QQ多少
风之影子 2009-08-17
  • 打赏
  • 举报
回复
你把提取的代码

提取二维数组里的特定维的某个无素
MessageBox.Show(identify[1, 5]);



里的坐标改了就行了。
fenghy2008 2009-08-17
  • 打赏
  • 举报
回复
libinguest先感谢你,在控件中显示矩阵这部分没问题。但是现在如果要求用户根据显示的矩阵输入坐标值,然后返回对应的坐标值的内容,则应该如何实现呢?
例如:界面上有个textbox,用户根据显示的矩阵: 你 我 他
请 好 写
是 可 有
输入坐标(1,1) 则返回矩阵中对应的--"好"这个字符串。也就是说用户输入坐标然后返回矩阵中对应的值。 麻烦帮忙看看应该怎样才能实现呢?谢谢。
happyboyxq1985 2009-08-17
  • 打赏
  • 举报
回复
6楼的基础上改一下
private static string[][] GenArray(string[] input, int x, int y)
{
if (x < 1 || y < 1)
{
throw new ArgumentException("Array dimensions must be bigger than 1");
}
if (input.Length != x * y)
{
throw new ArgumentException("Bad input string");
}
List<string> temp = new List<string>(input);

string[][] result = new string[x][];

for (int i = 0; i < x; i++)
{
result[i] = new string[y];
for (int j = 0; j < y; j++)
{
Random random = new Random();
int index = random.Next(0, temp.Count);
result[i][j] = temp[index];
temp.RemoveAt(index);
}
}

return result;
}


static void Main(string[] args)
{
string[] input = { "你", "我", "他", "它", "好", "为", "怀", "请", "学" };

while (true)
{
string[][] temp = GenArray(input, 3, 3);
Console.WriteLine("生成的新矩阵");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(temp[i][j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("请输入要显示的字符串索引:");
int x, y;
if (int.TryParse(Console.ReadLine(), out x) && int.TryParse(Console.ReadLine(), out y))
{
try
{
Console.WriteLine(temp[x][y]);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
}

cppfaq 2009-08-17
  • 打赏
  • 举报
回复
已修正
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication8 {
class Program {
static void Main(string[] args) {
string[] input = { "你", "我", "他", "它", "好", "为", "怀", "请", "学" };

while (true) {
string[][] temp = GenArray(input, 3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write(temp[i][j] + "\t");
}
Console.Write("\n");
}
Console.WriteLine("请输入要显示的字符串索引:");
int x, y;
if (int.TryParse(Console.ReadLine(), out x) && int.TryParse(Console.ReadLine(), out y)) {
try {
Console.WriteLine(temp[x][y]);
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
}
}

private static string[][] GenArray(string[] input, int x, int y) {
if (x < 1 || y < 1) {
throw new ArgumentException("Array dimensions must be bigger than 1");
}
if (input.Length != x * y) {
throw new ArgumentException("Bad input string");
}
List<string> temp = new List<string>(input);

string[][] result = new string[x][];

for (int i = 0; i < x; i++) {
result[i] = new string[y];
for (int j = 0; j < y; j++) {
Random random = new Random(DateTime.Now.Millisecond.GetHashCode());
int index = random.Next(0, temp.Count);
result[i][j] = temp[index];
temp.RemoveAt(index);
}
}

return result;
}
}
}
风之影子 2009-08-17
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
//初始化数组并进行随机排序
intoOrderByArray();
//提取二维数组里的特定维的某个无素
MessageBox.Show(identify[1, 5]);
}

#region 初始化并进行随机排序
private void intoOrderByArray()
{
this.richTextBox1.Text = "";//清空richtextbox

#region 初始化你的二维数组
identify = new string[9, 9]{{"教", "律", "医", "导", "音", "美", "游", "旅", "羽"},
{"师", "行", "生", "学", "乐", "术", "戏", "其", "球"},
{"去", "吧", "看", "哦", "人", "个", "恶", "我", "他"},
{"在", "中", "张", "值", "才", "从", "程", "吃", "是"},
{"数", "所", "耍", "说", "的", "大", "对", "到", "飞"},
{"放", "发", "非", "法", "副", "过", "给", "该", "故"},
{"就", "将", "家", "键", "了", "类", "来", "浪", "连"},
{"调", "同", "它", "和", "好", "后", "哈", "并", "帮"},
{"平", "拍", "派", "快", "可", "框", "宇", "要", "敏"}
};
#endregion

#region 二维数组转换为一维
int x = 0;
for (int count = 0; count < identify.GetLength(0); count++)
{
for (int countone = 0; countone < identify.GetLength(1); countone++)
{
StrArray[x] = identify[count, countone].ToString();
x++;
}
}
#endregion

#region 对一维数组进行随机排序
Random rd = new Random();
for (int count = 0; count < StrArray.Length; count++)
{
int tempCount = rd.Next(count, StrArray.Length);
string temp = StrArray[tempCount];
StrArray[tempCount] = StrArray[count];
StrArray[count] = temp;
}
#endregion

#region 用richtextbox输出
x = 0;
for (int count = 0; count < identify.GetLength(0); count++)
{
for (int countone = 0; countone < identify.GetLength(1); countone++)
{//在此也可使其又转化为二维。下面是直接写到richtextbox里了。
identify[count, countone] = StrArray[x];//这行是把转换为二维数组
this.richTextBox1.AppendText(StrArray[x]);
this.richTextBox1.AppendText("\t");
x++;
}
this.richTextBox1.AppendText("\n\n");
}
#endregion
}
#endregion
fenghy2008 2009-08-17
  • 打赏
  • 举报
回复
在。
风之影子 2009-08-17
  • 打赏
  • 举报
回复
在吗?
fenghy2008 2009-08-17
  • 打赏
  • 举报
回复
cppfaq你好!
刚才运行了一下你写的程序,发现有点问题。没有显示加载的矩阵。而且当输入相同的索引每次的结果都是一样的。也就是说矩阵没有变化。高手能否再看一下?谢谢。
fenghy2008 2009-08-17
  • 打赏
  • 举报
回复
libinguest你好!能否把你的实现过程发给我?谢谢
风之影子 2009-08-17
  • 打赏
  • 举报
回复
楼主:其实我昨天写的就已经满足你现在的需求了。

后面我不是又发了一个把一维再转化为二维吗?

至于如何从二维数组里取数。
这个你应该没问题吧!
风之影子 2009-08-17
  • 打赏
  • 举报
回复
上面的控制台程序就行。

还要我的吗?
cppfaq 2009-08-17
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication8 {
class Program {
static void Main(string[] args) {
string[] input = { "你", "我", "他", "它", "好", "为", "怀", "请", "学" };

while (true) {
string[][] temp = GenArray(input, 3, 3);
Console.WriteLine("请输入要显示的字符串索引:");
int x, y;
if (int.TryParse(Console.ReadLine(), out x) && int.TryParse(Console.ReadLine(), out y)) {
try {
Console.WriteLine(temp[x][y]);
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
}
}

private static string[][] GenArray(string[] input, int x, int y) {
if (x < 1 || y < 1) {
throw new ArgumentException("Array dimensions must be bigger than 1");
}
if (input.Length != x * y) {
throw new ArgumentException("Bad input string");
}
List<string> temp = new List<string>(input);

string[][] result = new string[x][];

for (int i = 0; i < x; i++) {
result[i] = new string[y];
for (int j = 0; j < y; j++) {
Random random = new Random(x * y);
int index = random.Next(0, temp.Count);
result[i][j] = temp[index];
temp.RemoveAt(index);
}
}

return result;
}
}
}
加载更多回复(5)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧