Generating Random Number in the Array


This is for beginners. I've written this Game in C#. It's generating a random number in the array. So when the user enters the number, it check for that number in the index 0 of the array since it's generating only 1 value. So length of array is 1.

using System;
public class B
{
Random r =
new Random();
public string st;
Public int lukynum;
public virtual void PlayGame()
{
int [] scores = {r.Next(1,7)};
Console.Write("Type start to play the game: " );
st = Console.ReadLine();
try
{
if (st == "start")
{
do
{
Console.Write("Please Enter your lucky Number or -1 to Exit: ");
lukynum = Convert.ToInt16(Console.ReadLine());
Console.WriteLine();
if (lukynum == -1)
{
break;
}
for ( int i=0; i<scores.Length; i++)
{
if (scores[i] == lukynum)
{
Console.WriteLine("You won by chooing : " + scores[i]);
Console.WriteLine();
break ;
}
else
{
Console.WriteLine("\n\t****PLEASE TRY AGAIN!****");
Console.WriteLine("Lucky Number is between 1 and 7 ;) \n");
}
}
}
while (lukynum!=-1);
}
else
{
PlayGame();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public class C : B
{
public C()
{
base .PlayGame();
}
}
public class MyClient
{
public static void Main()
{
C c =
new C();
}
}


Recommended Free Ebook
Similar Articles