时间:2021-05-20
不过我写的比较草率,代码结构不是很好,也没有体现OOP的思想,这几天有空会重构一下。
先把代码发出来:
复制代码 代码如下:
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;
}
}
调用:
复制代码 代码如下:
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();
}
效果:
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
效果如下:C#实现代码?12345678910111213141516171819202122232425262728293031323334353637usi
淘宝密保卡的使用注意事项。步骤一:保存电子密保卡图片到电脑步骤二:保存成其他形式(正确的保存方法见下面漫画所示)步骤三:删除电脑里面的密保卡图片
本文展示了C#实现获取一年中是第几个星期的方法,对初学者学习C#时间操作有一定的借鉴价值,具体实现代码如下:主要功能代码如下://////获取日期是一年中第几个
本文所述实例为C#动态加载一张图片并显示及动态移除它的实现方法,代码主要涉及一些C#图像操作知识,代码简单易懂,对C#的初学者有一定的帮助。主要功能代码如下:u
官方提供了curl、post、php、ruby的实现示例,并没有C#的官方示例。既然提供了post的方式,那么就可以用C#实现,下面是实现代码:ASP.net百