【发布时间】:2020-03-06 06:44:47
【问题描述】:
我不知道之前是否已经回答过这个问题,但是如何使用箭头键移动二维数组中的元素?说这样的话,如果不可能,就让它不动,因为在显示的图像中 0 可以交换并向上、向下、向左和向右,但下一步你只能使用向上、向下和向左三个选项
C# 新手,所以如果网上有关于我的问题的有用示例,请将它们发布在评论中
using System;
namespace moveElement
{
public class move
{
static void Main(string[] args)
{
int[,] arr = { { 9, 1, 4 }, { 5, 0, 3 }, { 6, 8, 2 } };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
接受建议并让它像这样工作
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.RightArrow)
{
int temp = arr[1, 1];
arr[1, 1] = arr[1, 2];
arr[1, 2] = temp;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
在处理代码一段时间后,我在第 41 行(y)和 47(x)上收到错误 "use of unassigned local variable x and y",我已经完成了大部分问题,但仍然遇到此错误
using System;
namespace moveElement
{
public class move
{
static void Main(string[] args)
{
int x, y;
int[,] arr = { { 0, 1, 4 }, { 3, 9, 5 }, { 6, 8, 2 } };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr[i, j] + " ");
通过为x 和y 赋予0 的值来解决问题。
【问题讨论】: