In: Computer Science
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with $25.
Solution:
Code:
Output:
Copyable Code:
using System;
//Main class
class MainClass {
//Main method
public static void Main (string[] args) {
//Declaration of variables
int number1,number2,number3;
int rand1,rand2,rand3;
int amount =25;
int i=1;
//Loop
do
{
//Get values
Console.WriteLine ("Enter 3 numbers between 1 and 10:");
Console.WriteLine("Enter the value of number1:");
number1=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value of number2:");
number2=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value of number3:");
number3=Convert.ToInt32(Console.ReadLine());
//Validation
if(number1>10 || number1<1)
{
Console.WriteLine("Renter the value of number1:");
number1=Convert.ToInt32(Console.ReadLine());
}
if(number2>10 || number2<1)
{
Console.WriteLine("Renter the value of number2:");
number2=Convert.ToInt32(Console.ReadLine());
}
if(number2>10 || number2<1)
{
Console.WriteLine("Renter the value of number1:");
number1=Convert.ToInt32(Console.ReadLine());
}
//Generate random values
Random r=new Random();
rand1=r.Next(1,10);
rand2=r.Next(1,10);
rand3=r.Next(1,10);
//Check with lottory values
if(number1==rand1 && number2==rand2 && number3==rand3)
{
Console.WriteLine ("Congrats!!Won on the lottery");
Console.WriteLine("Got $10");
amount=amount+10;
}
else if((number1==rand1 && number2==rand2)||(number2==rand2 && number3==rand3)||(number3==rand3 && number1==rand1))
{
Console.WriteLine("Got $3");
amount=amount+3;
}
else{
Console.WriteLine("You lose the game");
amount=amount-5;
}
i=i+1;
}while(i<=5);
Console.WriteLine("Your final amount is:"+amount);
}
}