Hello! I see that you've created a lottery program in C#. It's a good start, but I'll help you fix the issues and guide you on how to compare the user input to the computer-generated winning number.
Here are the changes you need to make:
The primary issue is with how you read the user input. Instead of using Console.Read(), you should use int.Parse(Console.ReadLine()) to read a full integer input from the user.
The loop for generating the random numbers should be outside the loop for user input. Otherwise, you are generating a new random number in each iteration of the user input loop, which will result in different numbers every time.
Since you want a 5-digit number, you need to create an array to store each digit of the user input, as well as the winning number.
You need to compare each digit of the user input to the corresponding digit of the winning number.
Here's the modified code:
using System;
namespace gambling
{
class Program
{
public static void Main(string[] args)
{
int Match = 0;
int[] userNumber = new int[5];
int[] winningNumber = new int[5];
Console.WriteLine("Type a number with 5 digits");
// Read user input and store each digit in the array
for (int a = 0; a < 5; a++)
{
userNumber[a] = int.Parse(Console.ReadLine());
}
Console.Write("The winning number is ");
// Generate the winning number and store each digit in the array
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
winningNumber[i] = rnd.Next(0, 10);
Console.Write(winningNumber[i]);
}
// Compare user input with the winning number and count matches
for (int i = 0; i < 5; i++)
{
if (userNumber[i] == winningNumber[i])
{
Match += 1;
}
}
Console.WriteLine();
Console.WriteLine("You got " + Match + " numbers right.");
// Display the corresponding prize based on the number of matches
if (Match == 0)
{
Console.WriteLine("Better luck next time!");
}
else if (Match == 1)
{
Console.WriteLine("Good, but could be a bit better!");
}
else if (Match == 2 || Match == 3)
{
Console.WriteLine("Great! You just won 100 dollars!");
}
else if (Match == 4)
{
Console.WriteLine("Amazing! You are now 10,000 dollars richer!");
}
else if (Match == 5)
{
Console.WriteLine("Congratulations! You won the main prize of 1,000,000 dollars!");
}
else
{
Console.WriteLine("What the heck");
}
Console.ReadLine();
}
}
}
Now, the code will read the user input correctly, generate a random winning number, compare the two, and provide the appropriate result. It should work as expected. If you have any further questions or need more assistance, feel free to ask. Good luck with your school project!