Domanda

'life' starts out as 12, and 'randomNumberhit' is a random number 1,2 or 3; 'randomNumberhit' is taken away from 'life' but when I try to recognise that 'life' is 0, -1 or -2 it does not work.

int life = 12;
...
Random randomhit = new Random();
int randomNumberhit = randomhit.Next(1, 4);
...
life = life - randomNumberhit;
Console.WriteLine(life);
if (life == 0)
{
Console.Writeline("end 0");
}
if (life == -1)
{
Console.Writeline("end -1");
}
if (life == -2)
{
Console.Writeline("end -2");
}

Even when 'life' is 0, -1 or -2 it does not follow a path, any help would be much appreciated.

È stato utile?

Soluzione 2

I would use if/else if like the comment above. But could you not just use;

   if (life <= 0)

Altri suggerimenti

What is the value of randomNumberhit ? It seems to be a number between 1 and 4. Let's say you have the maximum random value, which is 3. 12 - 3 > 0, which is why your if-statements will never evaluate to true.

Also, read MSDN: Random.Next.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top