Create a lottery game application. Generate four random numbers, each between 1 and 10. Allow the user to guess four numbers. 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
Make certain that your program accommodates repeating number. Save the file as Lottery.cs.
Full Answer Section
Console.WriteLine("Please enter your four guesses (separated by spaces):");
string[] userGuesses = Console.ReadLine().Split(' ');
int oneMatchCount = 0, twoMatchCount = 0, threeMatchCount = 0, fourMatchCount = 0;
bool exactMatch = true;
for (int i = 0; i < randomNumbers.Length; i++) {
for (int j = 0; j < userGuesses.Length; j++) {
if (randomNumbers[i] == Convert.ToInt32(userGuesses[j])) {
oneMatchCount++;
if (i == j) {
exactMatch = false;
}
break;
}
}
}
for (int i = 1; i < randomNumbers.Length; i++) {
if (twoMatchCount < 2 && randomNumbers[0] == Convert.ToInt32(userGuesses[i])) {
twoMatchCount++;
}
}
for (int i = 2; i < randomNumbers.Length; i++) {
if (threeMatchCount < 3 && randomNumbers[0] == Convert.ToInt32(userGuesses[i])) {
threeMatchCount++;
}
}
for (int i = 0; i < randomNumbers.Length; i++) {
if (fourMatchCount < 4 && randomNumbers[i] == Convert.ToInt32(userGuesses[i])) {
fourMatchCount++;
}
}
int winnings = 0;
if (fourMatchCount == 4 && exactMatch) {
winnings = 10000;
} else if (fourMatchCount == 4) {
winnings = 1000;
} else if (threeMatchCount == 3) {
winnings = 100;
} else if (twoMatchCount == 2) {
winnings = 30;
} else if (oneMatchCount == 1) {
winnings = 10;
}
Console.WriteLine("Your guess: " + String.Join(", ", userGuesses));
Console.WriteLine("Random numbers: " + String.Join(", ", randomNumbers));
Console.WriteLine("Your winnings: $" + winnings);
}
}
Save this code as Lottery.cs
and run it using the C# compiler.
Sample Answer
using System;
public class Lottery {
public static void Main(string[] args) {
int[] randomNumbers = new int[4];
Random random = new Random();
for (int i = 0; i < randomNumbers.Length; i++) {
randomNumbers[i] = random.Next(1, 11);