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
  • Fix program errors and improve code Visual Studio C# Console App (.NET Framework) Task The program...

    Fix program errors and improve code Visual Studio C# Console App (.NET Framework) Task The program must allow for the teacher to either enter a predetermined number of scores (e.g. 10 exam scores), to keep allowing them to enter in scores until they are finished. You will need to convert these scores to a percentage then store these in a list. You will perform some calculations on these results and also display the contents of the list (percentage values) back...

  • I need help with coding with c++. I need to make a lottery program where you...

    I need help with coding with c++. I need to make a lottery program where you enter in 5 numbers from 0-9 and the program will generate 5 random numbers 0-9 to represent the lottery numbers. I'm almost done, but the output isn't exactly what I want. It works fine, but the format should look like this: lottery array: 7 4 9 1 3 user array: 4 2 9 7 p.s. I also need to count how many matching numbers...

  • Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through...

    Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through 9 and put this in an array. Then these user numbers are compared to the random numbers the computer generated. The program will display both the computer random number and below the user selected numbers. The program will tell the user how many matches are made. If the user guesses all five you let them know they are the grand winner. Here is the...

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

  • in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should...

    in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in 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 element in the two arrays and keep a count of the digits that match. For...

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

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