c# - 创建修改二维数组元素的算法时遇到问题
问题描述
我在编辑二维字符数组的值时遇到问题。
char[,] chrRaster = new char[4, 5];
将值添加到数组并将其打印到控制台后,我得到:
// Input example:
*****
**.**
*****
****.
我正在尝试制作一种算法来替换“。”旁边、下方或上方的每个“*”。由“。” 然后将其打印到控制台。
// Output after algorithm example:
**.**
*...*
**.*.
***..
我尝试将 2d char 数组转换为 2d 字符串数组,然后IndexOf('*')
用于替换'*'
a 旁边、下方或上方的所有内容'.'
,并且我还尝试使用多个if
andfor
循环来计算它,但没有任何运气。
static void Main(string[] args)
{
// Variablen
int intTestgeval = 0; // Number of times you want program to repeat
int intN = 0; // Number of rows
int intM = 0; // Number of coloms
char chrGrond; // Used to store '*' or '.'
char[,] chrRaster; // 2d char array used to store all values
// Code
try
{
intTestgeval = Int32.Parse(Console.ReadLine()); // Number of times program will repeat
if(intTestgeval > 150) // Program can not repeat more then 150 times
{
throw new Exception();
}
}
catch (Exception)
{
Environment.Exit(0);
}
intN = Controle(intN); // Number of rows ophalen
intM = Controle(intM); // Number of Coloms ophalen
chrRaster = new char[intN, intM]; // Initializing array with user input
for (int intR = 0; intR < intTestgeval; intR++) // Print 2d array to console
{
for(int intY = 0; intY < intN; intY++)
{
for(int intZ = 0; intZ < intM; intZ++)
{
chrGrond = Convert.ToChar(Console.ReadKey().KeyChar);
chrRaster[intY, intZ] = chrGrond;
}
Console.WriteLine();
}
instorten[intR] = Instorten(chrRaster, intN, intM); // Ignore this part, that's another part of my assignment not related to my question.
}
}
static int Controle( int intX )
{
try
{
intX = Int32.Parse(Console.ReadLine());
if (intX > 150 || intX < 1) // Length of row and colom can not exceed 20 and can not go lower then 1
{
throw new Exception();
}
return intX;
}
catch // Program will off if value does not meet requirements
{
Environment.Exit(0);
return intX;
}
}
// It is this part of the program I need help with. This is what I tried but can't get any further
static int Instorten(char[,] chrRaster, int intN, int intM)
{
for (int intY = 0; intY < intN; intY++)
{
for (int intZ = 0; intZ < intM; intZ++)
{
if(chrRaster[intY, intZ] == '.' && chrRaster[intY, intZ + 1] == '*' || chrRaster[intY, intZ] == '*' && chrRaster[intY, intZ + 1] == '.')
{
}
}
Console.WriteLine();
}
int intm = 0;
return intm;
}
}
解决方案
这是执行您想要的算法。我试图在评论中解释我的代码。输出将匹配您要查找的内容。
static void Main(string[] args)
{
char STAR = '*';
char DOT = '.';
var input = new char[,]
{
{ STAR,STAR,STAR,STAR,STAR},
{ STAR,STAR,DOT,STAR,STAR},
{ STAR,STAR,STAR,STAR,STAR},
{ STAR,STAR,STAR,STAR,DOT}
};
var output = new char[4, 5];
// Copy each from input to output, checking if it touches a '.'
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 5; y ++)
{
if (input[x, y] == STAR)
{
var isDot = false;
// Check left
if (x > 0)
isDot = input[x - 1, y] == DOT;
// Check right
if (x < 3)
isDot = isDot || (input[x + 1, y] == DOT);
// Check above
if (y > 0)
isDot = isDot || (input[x, y - 1] == DOT);
// Check below
if (y < 4)
isDot = isDot || (input[x, y + 1]) == DOT;
output[x, y] = isDot ? DOT : STAR;
}
else
{
output[x, y] = input[x, y];
}
}
}
// Print output
for (int x = 0; x < 4; x ++)
{
for (int y = 0; y < 5; y ++)
{
Console.Write(output[x, y]);
}
Console.WriteLine();
}
Console.Read();
}
推荐阅读
- java - Java中数组的总和
- tensorflow - 是否可以将已经训练好的 Keras 部署到 Sagemaker?
- javascript - 如何让两个变量始终等于一个恒定比率?
- javascript - 关闭firebase监听器的正确方法?
- javascript - React-Apollo:对变量列表进行查询调用
- php - 如何在遗留 PHP 页面中集成 Symfony 控制器操作?
- javascript - Fancybox3 高度默认为 9999px
- r - 从 R 中的光栅进程中定位临时文件:缺少 140 Gb
- excel - VBA插入公式以将单元格的值添加到另一个
- html - 在我的网页中,一些字符在上传到主机时发生了变化,但在本地 Asp.net MVC 中很好