我正在尝试使此循环工作,因此它将继续询问数字,直到用户输入999.但是,使用此版本的代码,没有建立说我不能在循环中声明num,因为我在这个范围内赋予它不同的含义。在'while循环'中声明变量,但在循环期间不能更改值
使用try和catch是因为我的这段代码的分配规则。
int num;
while (num != 999)
{
Console.WriteLine("Enter a number between 0 and 99");
string input = Console.ReadLine();
try
{
int num = Convert.ToInt32(input);
Console.WriteLine("This is element number " + num + " : " + randNums[num]);
}
catch
{
Console.WriteLine("Data inputted is not between 0 and 99");
}
}
Console.WriteLine("You chose the secret value, well done!");
只需从第二个'num'前删除'int' – paqogomez
双声明... –
并使用单位变量编号..... – Steve