各位高手朋友你们好!
小弟现有一问题不知道如何解决,希望各位高手能多多帮忙,给点意见。问题描述如下:
一个集合为{"你","我","他","它","好","为","怀","请","学"},然后初始化string[3,3]的二维数组。要求每次初始化的结果不同,进而显示的矩阵结果也不同。例如:第一次初始化string[3,3]={"你","我","他","它","好","为","怀","请","学"} 从而显示的矩阵形式为:你 我 他
它 好 为
怀 请 学而第二次初始化string[3,3]={"好","学","你","请","怀","我","它","为","他"}进而得到的矩阵为:好 学 你
请 怀 我
它 为 他 也就是说初始化二维数组不同,进而显示的矩阵也就不同。同时,当用户输入矩阵的坐标时,能够返回其坐标对应的内容。例如:你 我 他 这个3*3的矩阵,当用户输入坐标(1,1)则返回对应的--"好"。
它 好 为
怀 请 学
请各位高手们多多帮忙,最好能提供思路和实现源码。万分感激!!
小弟现有一问题不知道如何解决,希望各位高手能多多帮忙,给点意见。问题描述如下:
一个集合为{"你","我","他","它","好","为","怀","请","学"},然后初始化string[3,3]的二维数组。要求每次初始化的结果不同,进而显示的矩阵结果也不同。例如:第一次初始化string[3,3]={"你","我","他","它","好","为","怀","请","学"} 从而显示的矩阵形式为:你 我 他
它 好 为
怀 请 学而第二次初始化string[3,3]={"好","学","你","请","怀","我","它","为","他"}进而得到的矩阵为:好 学 你
请 怀 我
它 为 他 也就是说初始化二维数组不同,进而显示的矩阵也就不同。同时,当用户输入矩阵的坐标时,能够返回其坐标对应的内容。例如:你 我 他 这个3*3的矩阵,当用户输入坐标(1,1)则返回对应的--"好"。
它 好 为
怀 请 学
请各位高手们多多帮忙,最好能提供思路和实现源码。万分感激!!
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;
}
}
}
刚才运行了一下你写的程序,发现有点问题。没有显示加载的矩阵。而且当输入相同的索引每次的结果都是一样的。也就是说矩阵没有变化。高手能否再看一下?谢谢。
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
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;
}
}
}
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);
}
}
}
}
}
例如:界面上有个textbox,用户根据显示的矩阵: 你 我 他
请 好 写
是 可 有
输入坐标(1,1) 则返回矩阵中对应的--"好"这个字符串。也就是说用户输入坐标然后返回矩阵中对应的值。 麻烦帮忙看看应该怎样才能实现呢?谢谢。
MessageBox.Show(identify[1, 5]);
里的坐标改了就行了。
首先感谢你提供的实现源码,在控制台下运行后没问题。但是我最初的想法是通过界面来实现,即将每次加载显示的矩阵通过winform中的控件加载后在界面上显示,并且提供一个textbox文本框,接收用户输入的矩阵的坐标值,然后返回矩阵中对应的某个字符串。也就是说想把在控制台下实现的整个过程通过界面形式完成。所以能否麻烦高手再帮帮忙看看如何通过界面的形式实现?万分感谢!
你所提供的代码能实现通过richtextbox每次加载显示不同的矩阵结果。如果现在界面上有一个textbox文本框,用来接收用户输入的矩阵坐标值,然后返回矩阵中对应的字符串。这个过程如何实现呢?麻烦你帮帮忙,谢谢!!