(C# Programming)Create a lottery game application. 1.Generate four random number
ID: 3723819 • Letter: #
Question
(C# Programming)Create a lottery game application.
1.Generate four random numbers, each between 1 and 10.(Make sure they are displayed for user to see)
2.Allow the user to guess four numbers.
3.Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four numbers, and amount of money the user has won as follows: Any One matching: $10 Two matching: $30 Three matching: $100 Four matching, not in order: $1000 Four matching, in exact order: $10000 No matches: $0
4.MAKE CERTAIN THAT RANDOM NUMBERS GENERATED CAN NOT BE SAME AS OTHER 3 RANDOM NUMBERS.(IN OTHER WORDS NO REPEATING NUMBERS)
5. Display message on how many numbers match(if any) and display either winning message or loss message.
Hint: Random RanNumber= new Random(); Int ran1; ran1= RanNumber.Next(min,max);
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Taking min and max values i.e range between the random numbers
int min = 1, max = 10;
//Initialisation of used variavles
Random RanNumber = new Random();
List<int> randomList = new List<int>();
List<int> userList = new List<int>();
int ran1;
//Taking the random numbers and storing in a list
while (randomList.Count() < 4)
{
ran1 = RanNumber.Next(min, max);
if (!randomList.Contains(ran1))
{
randomList.Add(ran1);
}
}
//User prompt to take 4 numbers in space seperated EX: 1 2 3 4
Console.WriteLine("Guess 4 numbers: ");
var input = Console.ReadLine(); //To read input from console
//making a list from the taken input
var integers = input.Split(new Char[] { ' ' }).Select(x => Convert.ToInt32(x)).ToList();
userList = integers.Distinct().ToList(); //Getting distinct set of values
//Printing the values
Console.WriteLine(" User defined Numbers are: ");
foreach (int item in integers)
{
Console.WriteLine("{0}", item);
}
Console.WriteLine(" Random Numbers are: ");
foreach (int item in randomList)
{
Console.WriteLine("{0}", item);
}
var a = randomList.SequenceEqual(integers); //To check all the elements in sequence or not
if (!a)
{
int count = 0;
for (int i = 0; i < userList.Count(); i++)
{
if (randomList.Contains(userList[i]))
{
count++;
}
}
if (count == 0)
{
Console.WriteLine("0 matches and You loss the match and won $0");
}
else if (count == 1)
{
Console.WriteLine("1 match and You won $10");
}
else if (count == 2)
{
Console.WriteLine("2 match and You won $30");
}
else if (count == 3)
{
Console.WriteLine("3 match and You won $100");
}
else
{
Console.WriteLine("4 match and You won $1000");
}
}
else
{
Console.WriteLine("Hurray all 4 numbers are correctly matched in sequence and You have won $10000");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.