for循环和elseif之间变量的c#逼近

for循环和elseif之间变量的c#逼近,c#,C#,快速提问。。我需要到达for循环中随机类的实例rand。 我错过了什么成功的机会 Console.WriteLine("Seedwaarde (optioneel)"); int s = Convert.ToInt16(Console.ReadLine()); if (s != 0 || s != null) { Random rand = new Random(s); } else { Random rand = new Random(); } for(int i =

快速提问。。我需要到达for循环中随机类的实例rand。 我错过了什么成功的机会

Console.WriteLine("Seedwaarde (optioneel)");
int s = Convert.ToInt16(Console.ReadLine());

if (s != 0 || s != null)
{

   Random rand = new Random(s);

}
else
{

    Random rand = new Random();

}

for(int i = 0;i < 20; i++){

    rand.next(1, 13);

}
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
如果(s!=0 | | s!=null)
{
随机随机数=新随机数;
}
其他的
{
Random rand=新的Random();
}
对于(int i=0;i<20;i++){
兰德·奈特(1,13);
}

您必须在外部范围内声明
rand
变量。我已删除
s!=null
检查,因为
int
不能为
null

Console.WriteLine("Seedwaarde (optioneel)");

int s = Convert.ToInt16(Console.ReadLine());
Random rand;

if (s != 0)
{
   rand = new Random(s);
}
else
{
    rand = new Random();
}

for(int i = 0;i < 20; i++){
    rand.next(1, 13);
}

您必须在外部范围中声明
rand
变量。我已删除
s!=null
检查,因为
int
不能为
null

Console.WriteLine("Seedwaarde (optioneel)");

int s = Convert.ToInt16(Console.ReadLine());
Random rand;

if (s != 0)
{
   rand = new Random(s);
}
else
{
    rand = new Random();
}

for(int i = 0;i < 20; i++){
    rand.next(1, 13);
}

您必须在打算使用的范围内定义
rand

Console.WriteLine("Seedwaarde (optioneel)");
int s = Convert.ToInt16(Console.ReadLine());

Random rand;
if (s != 0 || s != null /*note this second condition will never be true*/)
{
   rand = new Random(s);
}
else
{
    rand = new Random();
}

for(int i = 0;i < 20; i++)
{
    rand.next(1, 13);
}
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null/*请注意,第二个条件永远不会为真*/)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++)
{
兰德·奈特(1,13);
}


在问题中发布的代码片段中,您在每个if块和else块中定义了一个新的本地
rand
,一旦该块退出,它就会立即超出范围,然后很快就会找到垃圾箱。

你必须在你打算使用它的范围内定义
兰德

Console.WriteLine("Seedwaarde (optioneel)");
int s = Convert.ToInt16(Console.ReadLine());

Random rand;
if (s != 0 || s != null /*note this second condition will never be true*/)
{
   rand = new Random(s);
}
else
{
    rand = new Random();
}

for(int i = 0;i < 20; i++)
{
    rand.next(1, 13);
}
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null/*请注意,第二个条件永远不会为真*/)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++)
{
兰德·奈特(1,13);
}


在问题中发布的代码片段中,您在每个if块和else块中定义了一个新的本地
rand
,一旦该块退出,它就会立即超出范围,然后很快就会找到垃圾箱。

只需将rand变量声明在if语句的范围之外

    Console.WriteLine("Seedwaarde (optioneel)");
    int s = Convert.ToInt16(Console.ReadLine());
    Random rand;
    if (s != 0 || s != null)
    {

      rand = new Random(s);

    }
    else
    {

        rand = new Random();

    }

    for(int i = 0;i < 20; i++){

        rand.next(1, 13);

    }
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++){
兰德·奈特(1,13);
}

只需将rand变量声明在if语句的范围之外

    Console.WriteLine("Seedwaarde (optioneel)");
    int s = Convert.ToInt16(Console.ReadLine());
    Random rand;
    if (s != 0 || s != null)
    {

      rand = new Random(s);

    }
    else
    {

        rand = new Random();

    }

    for(int i = 0;i < 20; i++){

        rand.next(1, 13);

    }
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++){
兰德·奈特(1,13);
}
早点声明

    Console.WriteLine("Seedwaarde (optioneel)");
    int s = Convert.ToInt16(Console.ReadLine());
    Random rand;
    if (s != 0 || s != null)
    {

       rand = new Random(s);

    }
    else
    {

        rand = new Random();

    }

    for(int i = 0;i < 20; i++){

        rand.next(1, 13);

    }
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++){
兰德·奈特(1,13);
}
早点声明

    Console.WriteLine("Seedwaarde (optioneel)");
    int s = Convert.ToInt16(Console.ReadLine());
    Random rand;
    if (s != 0 || s != null)
    {

       rand = new Random(s);

    }
    else
    {

        rand = new Random();

    }

    for(int i = 0;i < 20; i++){

        rand.next(1, 13);

    }
Console.WriteLine(“Seedwaarde(optioneel)”;
int s=Convert.ToInt16(Console.ReadLine());
随机兰德;
如果(s!=0 | | s!=null)
{
兰德=新的随机数;
}
其他的
{
rand=新随机数();
}
对于(int i=0;i<20;i++){
兰德·奈特(1,13);
}

int
永远不能为
null
为什么希望用户输入种子值?@stuartd为什么不?例如,一个地图种子(想想Minecraft)怎么样?@lc。与随机种子相比,它有什么优势?(我把Minecraft留给了我7岁的儿子.)-这是为了让这一代可以重复吗?@stuartd如果你找到一张你喜欢的地图(有趣的功能,方便的起点),你可以得到它的种子,并与其他人分享,然后这些人可以为自己生成相同的地图并进行游戏。FWIW,您可以签出
int
不能为
null
为什么希望用户输入种子值?@stuartd为什么不?例如,一个地图种子(想想Minecraft)怎么样?@lc。与随机种子相比,它有什么优势?(我把Minecraft留给了我7岁的儿子.)-这是为了让这一代可以重复吗?@stuartd如果你找到一张你喜欢的地图(有趣的功能,方便的起点),你可以得到它的种子,并与其他人分享,然后这些人可以为自己生成相同的地图并进行游戏。FWIW,你可以退房,对不起,没有预约;在变量声明的末尾。抱歉,没有;在变量声明的末尾。结束括号后的//comment不是比语句中的//**/comment更可取吗?@stuartd我想将它与条件本身内联,但你可能是对的。结束括号后的//comment不是比语句中的//**/comment更可取吗?@stuartd我想把它与条件本身联系起来,但你可能是对的。