Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(C#, Visual Studio) I need help modifying my program code to accommodate repeated numbers. For example, if a user inputs a repeated number the program

(C#, Visual Studio)

I need help modifying my program code to accommodate repeated numbers. For example, if a user inputs a repeated number the program should regenerate or ask to enter again.

If needed, here is the program requirements:

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 users guesses to the four random numbers and display a message that includes the users 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. Hint: Random RanNumber= new Random(); Int ran1; ran1= RanNumber.Next(min,max);

My program code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Lottery

{

class Lottery

{

static void Main(string[] args)

{

// This will generate the 4 random numbers input by the user

Random RanNumber = new Random();

int ran1 = RanNumber.Next(1, 10);

int ran2 = RanNumber.Next(1, 10);

int ran3 = RanNumber.Next(1, 10);

int ran4 = RanNumber.Next(1, 10);

int temporary1 = ran1, temporary2 = ran2, temporary3 = ran3, temporary4 = ran4;

// This will prompt the user to input the 4 numbers and read the users input

Console.WriteLine(" Guess four numbers between 1 and 10:");

int userGuess1, userGuess2, userGuess3, userGuess4;

// This will convert the string reperesentation of a number to a 32-bit signed integer

userGuess1 = Int32.Parse(Console.ReadLine());

userGuess2 = Int32.Parse(Console.ReadLine());

userGuess3 = Int32.Parse(Console.ReadLine());

userGuess4 = Int32.Parse(Console.ReadLine());

int temporaryGuess1 = userGuess1, temporaryGuess2 = userGuess2, temporaryGuess3 = userGuess3, temporaryGuess4 = userGuess4;

// This if statement will help with handling duplicate numbers that are entered and compared

if (ran1 == ran2)

ran1 = 0;

if (ran1 == ran3)

ran1 = 0;

if (ran1 == ran4)

ran1 = 0;

if (ran2 == ran3)

ran2 = 0;

if (ran2 == ran4)

ran2 = 0;

if (ran3 == ran4)

ran3 = 0;

// This will help with reading the numbers entered by the user to determine if the numbers match

if (userGuess1 == userGuess2)

userGuess1 = 0;

if (userGuess1 == userGuess3)

userGuess1 = 0;

if (userGuess1 == userGuess4)

userGuess1 = 0;

if (userGuess2 == userGuess3)

userGuess2 = 0;

if (userGuess2 == userGuess4)

userGuess2 = 0;

if (userGuess3 == userGuess4)

userGuess3 = 0;

int numbersMatched = 0;

bool inOrder = false;

// This will check for matches once the user inputs their guess and the numbers that are randomly selected

if (userGuess1 == ran1 || userGuess1 == ran2 || userGuess1 == ran3 || userGuess1 == ran4)

numbersMatched++;

if (userGuess2 == ran1 || userGuess2 == ran2 || userGuess2 == ran3 || userGuess2 == ran4)

numbersMatched++;

if (userGuess3 == ran1 || userGuess3 == ran2 || userGuess3 == ran3 || userGuess3 == ran4)

numbersMatched++;

if (userGuess4 == ran1 || userGuess4 == ran2 || userGuess4 == ran3 || userGuess4 == ran4)

numbersMatched++;

// This will check the order of the numbers that are input by the user

if (temporary1 == temporaryGuess1 && temporary2 == temporaryGuess2 && temporary3 == temporaryGuess3 && temporary4 == temporaryGuess4)

inOrder = true;

// This will determine the amount the user has won depending on the numbers input and order

int amountWon = 0;

if (numbersMatched == 0)

amountWon = 0;

if (numbersMatched == 1)

amountWon = 10;

if (numbersMatched == 2)

amountWon = 30;

if (numbersMatched == 3)

amountWon = 100;

if (numbersMatched == 4 && inOrder == false)

amountWon = 1000;

if (numbersMatched == 4 && inOrder == true)

amountWon = 10000;

// This will be the output display for the results

Console.WriteLine(" Results:");

Console.WriteLine(" Randomly generated numbers:" + temporary1 + "" + temporary2 + "" + temporary3 + "" + temporary4);

Console.WriteLine(" User Guesses:" + temporaryGuess1 + "" + temporaryGuess2 + "" + temporaryGuess3 + "" + temporaryGuess4);

Console.WriteLine(" Total numbers that matched:" + numbersMatched);

Console.WriteLine(" Total amount won: $" + amountWon);

Console.ReadLine();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions