Question

(C#, Visual Studio) I need help modifying my program code to accommodate repeated numbers. For example,...

(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 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.

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("\nGuess 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("\nResults:");

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

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

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

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

            Console.ReadLine();

        }

    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear student,

I have added the code where it asks the user to re-enter the values if they are same or repeated

also i have added similar code to random generated numbers where if any random number is repeated it will regenerate a new random number

by doing the above i thought of removing your code where duplicate values are made 0 because it will never be used as the numbers are unique and distinct.

please find the below 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);
while (ran1 == ran2)
{
ran2 = RanNumber.Next(1, 10);
}
int ran3 = RanNumber.Next(1, 10);
while (ran1 == ran3 || ran2 == ran3)
{
ran3 = RanNumber.Next(1, 10);
}
int ran4 = RanNumber.Next(1, 10);
while (ran1 == ran4 || ran2 == ran4 || ran3 == ran4)
{
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("\nGuess 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());
while (userGuess1 == userGuess2)
{
Console.WriteLine("\nPlease re-enter 2nd guess");
userGuess2 = Int32.Parse(Console.ReadLine());
}
userGuess3 = Int32.Parse(Console.ReadLine());
while (userGuess1 == userGuess3 || userGuess2==userGuess3)
{
Console.WriteLine("\nPlease re-enter 3nd guess");
userGuess3 = Int32.Parse(Console.ReadLine());
}
userGuess4 = Int32.Parse(Console.ReadLine());
while (userGuess1 == userGuess4 || userGuess2==userGuess4|| userGuess3==userGuess4)
{
Console.WriteLine("\nPlease re-enter 4nd guess");
userGuess4 = Int32.Parse(Console.ReadLine());
}
int temporaryGuess1 = userGuess1, temporaryGuess2 = userGuess2, temporaryGuess3 = userGuess3, temporaryGuess4 = userGuess4;

  

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("\nResults:");

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

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

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

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

Console.ReadLine();

}

}

}

Screenshot of output:

media%2F59a%2F59a6cb5f-2ec1-4f97-95fd-b1

If you have any doubt please comment below and also don't forget to hit the thumbs up button.

thank you :)

Add a comment
Know the answer?
Add Answer to:
(C#, Visual Studio) I need help modifying my program code to accommodate repeated numbers. For example,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello in C#. I need help understanding and knwoing what i missed in my program. The...

    Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Use visual studio C# Tasks You are going to build a program that generates a random...

    Use visual studio C# Tasks You are going to build a program that generates a random integer between 1 and 10, then prompts user to make several guesses of the number until user gets the right number - For each guess, tell user if the guess is higher or lower than the number - Handle the potential exception with user guess. Sample Output DAC programPrajects ExameExam1bin,DebugExarm1.exe Welcome! Guess the number I'm thinking of . It is between 1 and 1e...

  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • So I am creating a 10 question quiz in Microsoft Visual Studio 2017 (C#) and I...

    So I am creating a 10 question quiz in Microsoft Visual Studio 2017 (C#) and I need help editing my code. I want my quiz to clear the screen after each question. It should do one question at a time and then give a retry of each question that was wrong. The right answer should appear if the retry is wrong. After the 1 retry of each question, a grade should appear. Please note and explain the changes you make...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT