samedi 28 février 2015

C# console app random number generation

How can I get the random number generator method to loop and produce new random numbers? Do I need a separate class before Main that handles the random number generation? If so, how do I get the scope of those variables to Main? The console app is supposed to repeat integer math problems to practice with a loop that conditions on correct or incorrect answer. Also, what syntax am I overlooking to loop back to a new instance of randomly generated integers? TIA.



public class Multiplication
{
public static void Main(string[] args)
{
Random randomNumbers = new Random(); // random number generator
int num01 = randomNumbers.Next(1, 11);
int num02 = randomNumbers.Next(1, 11);
int value = 0;
while (true)
{
if (value != -1)
{
Console.Write("Please enter the answer to {0} x {1} = ?", num01, num02);
int product = num01 * num02;
Console.WriteLine();
int answer = Convert.ToInt32(Console.ReadLine());

while (product != answer)
{
Console.WriteLine("Incorrect, enter another guess: ");
answer = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Correct. Your answer was {0} \n {1} x {2} = {3} Very good!",
answer, num01, num02, product);
//keep console open
Console.WriteLine();
Console.WriteLine("Press - 1 to exit");
value = Convert.ToInt32(Console.ReadLine());
}
else
{
break;
}
}
}
}




Aucun commentaire:

Enregistrer un commentaire