【发布时间】:2016-06-21 15:35:23
【问题描述】:
只要我进入 while 循环,程序就会抛出 Index out of range 异常,而不会获取行和列的值。 getint 是一种在确保值在范围内的同时获取用户输入的方法
while(filled>0)
{
row = getint("row: ");
row--;
column = getint("column: ");
column--;
fields[row, column]=getint("value: ");
filled--;
}
编辑:
static int getint(string question)
{
string temp;
int tempint=0;
while (check != 0)
{
check = 0;
Console.Write("\nPlease enter the {0}",question);
temp = Console.ReadLine();
if(!int.TryParse(temp, out tempint))
{
check++;
}
if(question.Length<14 & (tempint<1 | tempint>9))
{
check++;
Console.WriteLine("Invalid Input");
}
}
return tempint;
}
【问题讨论】:
-
“不获取行和列值”是什么意思?我们不知道这些方法做了什么,也不知道
fields的大小是多少。请提供minimal reproducible example。 -
如果一进入while循环就抛出异常,那么问题可能出在
getint(),但你没有包含该代码。 -
需要更多代码。例如,这是否意味着从下到上阅读?
-
您使用了错误的运算符。您应该使用
&&和||而不是&和|。 -
你在哪里实例化
fields?