【问题标题】:Have a number correspond to a position in a multi dimensional array有一个数字对应于多维数组中的位置
【发布时间】:2022-07-01 19:27:01
【问题描述】:

我希望用户输入一个数字 1-9,并让该数字对应于 3x3 二维数组上的一个位置。然后将该数组中的值更改为“x”。

int input = Convert.ToInt32(Console.ReadLine());

string[,] numbers = { 
                    { " ", " ", " " },
                    { " ", " ", " " }, 
                    { " ", " ", " " }
                    };

起初我决定这样做:

int x = input % 3 - 1;
int y = input / 3 - 1;

然后在numbers[y, x] 访问数组,但这会导致索引超出范围的问题。

【问题讨论】:

  • 是的,当然。只要把数学弄好
  • 想想yinput 1 或2 得到的值是什么

标签: c#


【解决方案1】:
int x = input % 3 - 1;
int y = input / 3;

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 2012-10-16
    • 2019-04-12
    • 2014-01-07
    • 1970-01-01
    • 2017-09-04
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多