C#仿密??üδ艿暮?jiǎn)單實(shí)現(xiàn)代碼
不過我寫的比較草率,代碼結(jié)構(gòu)不是很好,也沒有體現(xiàn)OOP的思想,這幾天有空會(huì)重構(gòu)一下。
先把代碼發(fā)出來:
public class MatrixCardManager
{
public static int[,] ReadMatrixCardFromString(string matrixStr)
{
int[,] arr1 = new int[5, 5];
int[] tempArr = new int[25];
int k = 0;
string[] tempArrStr = matrixStr.Split(',');
for (int i = 0; i < tempArr.Length; i++)
{
tempArr[i] = Convert.ToInt32(tempArrStr[i]);
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arr1[i, j] = tempArr[k];
k++;
}
}
return arr1;
}
public static string SaveMatrixIntoString(int[,] arr)
{
string matrixStr = String.Empty;
int[] lineArr = new int[25];
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
lineArr[k] = arr[i, j];
k++;
}
}
for (int i = 0; i < lineArr.Length; i++)
{
matrixStr += lineArr[i];
if (i < 24)
{
matrixStr += ",";
}
}
return matrixStr;
}
public static void PrintMatrix(int[,] arr)
{
Console.WriteLine(" | A\tB\tC\tD\tE");
Console.WriteLine("-------------------------------------------");
for (int k = 0; k < 5; k++)
{
Console.Write(k + " | ");
for (int l = 0; l < 5; l++)
{
Console.Write(arr[k, l] + "\t");
}
Console.WriteLine();
}
}
public static int[,] GenerateRandomMatrix()
{
Random r = new Random();
int[,] arr = new int[5, 5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arr[i, j] = r.Next(0, 100);
}
}
return arr;
}
public static char GetColCode(int colIndex)
{
char colCode = '-';
switch (colIndex)
{
case 0:
colCode = 'A';
break;
case 1:
colCode = 'B';
break;
case 2:
colCode = 'C';
break;
case 3:
colCode = 'D';
break;
case 4:
colCode = 'E';
break;
default:
break;
}
return colCode;
}
public static bool Validate(int[,] arr, int colIndex1, int rowIndex1, int colIndex2, int rowIndex2, int colIndex3, int rowIndex3, string userInput, bool validFlag)
{
try
{
string[] inputArr = userInput.Split(',');
bool OK0 = arr[rowIndex1, colIndex1] == Convert.ToInt32(inputArr[0]);
bool OK1 = arr[rowIndex2, colIndex2] == Convert.ToInt32(inputArr[1]);
bool OK2 = arr[rowIndex3, colIndex3] == Convert.ToInt32(inputArr[2]);
if (OK0 && OK1 && OK2)
{
validFlag = true;
}
else
{
validFlag = false;
}
}
catch (Exception)
{
Console.WriteLine("Oh, **!");
}
return validFlag;
}
}
調(diào)用:
static void Main(string[] args)
{
Console.WriteLine("Generate and Print Matrix Card:\n");
int[,] arr = MatrixCardManager.GenerateRandomMatrix();
MatrixCardManager.PrintMatrix(arr);
Console.WriteLine("\n");
Console.WriteLine("Save Matrix Card into string for storage:\n");
string matrixStr = MatrixCardManager.SaveMatrixIntoString(arr);
Console.WriteLine(matrixStr);
Console.WriteLine("\n");
Console.WriteLine("Read Matrix Card from string:\n");
int[,] arr1 = MatrixCardManager.ReadMatrixCardFromString(matrixStr);
MatrixCardManager.PrintMatrix(arr1);
Console.WriteLine("\n");
Console.WriteLine("Matrix Card Validation:\n");
Random r = new Random();
int colIndex1 = r.Next(0, 4);
int rowIndex1 = r.Next(0, 4);
char colCode1 = MatrixCardManager.GetColCode(colIndex1);
int colIndex2 = r.Next(0, 4);
int rowIndex2 = r.Next(0, 4);
char colCode2 = MatrixCardManager.GetColCode(colIndex2);
int colIndex3 = r.Next(0, 4);
int rowIndex3 = r.Next(0, 4);
char colCode3 = MatrixCardManager.GetColCode(colIndex3);
Console.WriteLine("Please Input Card Number At {0}{1},{2}{3},{4}{5}:\n", colCode1, rowIndex1, colCode2, rowIndex2, colCode3, rowIndex3);
string userInput = Console.ReadLine();
bool validFlag = false;
validFlag = MatrixCardManager.Validate(arr, colIndex1, rowIndex1, colIndex2, rowIndex2, colIndex3, rowIndex3, userInput, validFlag);
if (validFlag)
{
Console.WriteLine("All input are correct!");
}
else
{
Console.WriteLine("Sorry, your input were wrong!");
}
Console.ReadKey();
}
效果:
相關(guān)文章
C# 創(chuàng)建Excel氣泡圖的實(shí)例代碼
這篇文章主要介紹了C# 創(chuàng)建Excel氣泡圖的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼
開發(fā)中有時(shí)會(huì)遇到要對(duì)文件進(jìn)行共享狀態(tài)的讀寫操作,代碼如下,需要的朋友可以參考下2012-06-06C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤文件變更的方法
這篇文章主要介紹了C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤文件變更的方法,詳細(xì)分析了FileSystemWatcher的用法,并以此為基礎(chǔ)實(shí)現(xiàn)監(jiān)視磁盤文件變更,是非常實(shí)用的技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法
這篇文章主要介紹了C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法,需要的朋友可以參考下2014-08-08C#編程中使用設(shè)計(jì)模式中的原型模式的實(shí)例講解
這篇文章主要介紹了C#編程中使用設(shè)計(jì)模式中的原型模式的實(shí)例講解,原型模式創(chuàng)建新對(duì)象方便快捷,而且可在運(yùn)行時(shí)根據(jù)需要通過克隆來添加和去除他們,也可在程序運(yùn)行是根據(jù)情況來修改類內(nèi)部的數(shù)據(jù),需要的朋友可以參考下2016-02-02C# 中的GroupBy的動(dòng)態(tài)拼接問題及GroupBy<>用法介紹
這篇文章主要介紹了C# 中的GroupBy的動(dòng)態(tài)拼接問題,在文章給大家提到了C# List泛型集合中的GroupBy<>用法詳解,需要的朋友可以參考下2017-12-12C#多線程學(xué)習(xí)之(四)使用線程池進(jìn)行多線程的自動(dòng)管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用線程池進(jìn)行多線程的自動(dòng)管理,實(shí)例分析了C#中線程池的概念與相關(guān)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#對(duì)桌面應(yīng)用程序自定義鼠標(biāo)光標(biāo)
這篇文章介紹了C#對(duì)桌面應(yīng)用程序自定義鼠標(biāo)光標(biāo)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取公共父節(jié)點(diǎn)的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取公共父節(jié)點(diǎn)的方法,需要的朋友可以參考下2014-08-08