110,534
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
}
}
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 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);
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;
}
}
}