samedi 28 mars 2015

Using random variable in other class gives always 0 c#

I haven't any errors, but the program always generates 0 for AnimalAge. It should generate numbers from 1-20. This is a simple game where a player should find Age of Animal.



public class losowaniez
{
public int AnimalAge;
public int Age2;
public void losowanie()
{
int AnimalAge;

Random RandomObj = new Random();
AnimalAge = RandomObj.Next(20);
Age2 = AnimalAge;
}
}
public class Animal:losowaniez
{
public void Fish()
{
Console.WriteLine("Zwierze moze miec maksymalnie 10 lat, zgadnij ile ma lat");
int x = Convert.ToInt32(Console.ReadLine());
}
public void zwierze()
{
losowaniez MyAnimal = new losowaniez();
int AnimalAge = MyAnimal.Age2;
Console.WriteLine("Zwierze moze miec maksymalnie 20 lat zgadnij ile ma lat");
int x = Convert.ToInt32(Console.ReadLine());
if (x == AnimalAge)
{
Console.WriteLine("Wygrales gre");
Console.ReadKey();
}
else if (x > AnimalAge)
{
Console.WriteLine("Celuj nizej!");
zwierze();
}
else
{
Console.WriteLine("Celuj wyzej!");
zwierze();
}
}
}




Aucun commentaire:

Enregistrer un commentaire