-
C#教程之C#仿密保卡功能的简单实现代码
不过我写的比较草率,代码结构不是很好,也没有体现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();
}
效果:
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式