Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • Create a console-based lottery game application - Help needed by anyone
AuthorQuestion
Create a console-based lottery game application - Help needed by anyone
Posted on: 10 Aug 2012

1.       Create a console-based lottery game application. Generate three random numbers, each between 1 and 4. Allow the user to guess three numbers. Compare each of the user's guesses to the three random numbers and display a message that includes the user's guess, the randomly deter-mined three-digit number, and the amount of money the user has won as follows:

Matching Numbers                                        Award ($)

Any one matching                                           10

Two matching                                                 100

Three matching, not in order                      1000

Three matching in exact order                   10,000

No matches                                                        0

Make certain that your application accommodates repeating digits. For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses—just one. Save the file as Lottery.cs.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Errors :  program work but give me different result ... i want result as above mansion in question help me its my 1st Question on this great  website

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace random

{

    class Program

    {

        static void Main(string[] args)

        {

            // declaring integers

            int iNum1;

            int iNum2;

            int iNum3;

            

            int iCount = 0;

            int iGuess;

            int iRandom;

            int iAmount;

            // declaring constants


            const int iBALLS = 3;

            const int iMATCHONE = 10;

            const int iMATCHTWO = 100;

            const int iMATCHTHREE = 1000;

            const int iMATCHFOUR = 10000;


            // array



            // generating random numbers


            Random randomnumber = new Random();


            iNum1 = randomnumber.Next(1, 4);

            iNum2 = randomnumber.Next(1, 4);

            iNum3 = randomnumber.Next(1, 4);

            


            iRandom = iNum1 + iNum2 + iNum3 ;


            for (iCount = 0; iCount < iBALLS; iCount++)

            {


                Console.Write("\nNo:" + (iCount + 1) + " Choose your number: ");

                iGuess = Convert.ToInt32(Console.ReadLine());



                while (iGuess < 1 || iGuess > 4)

                {

                    Console.WriteLine("You have entered an incorrect number please try again");

                    iGuess = Convert.ToInt32(Console.ReadLine());


                }


                iAmount = 0;


                if (iGuess == iNum1)

                {

                    Console.WriteLine("you won $" + iMATCHONE);

                    Console.WriteLine();

                    //Console.WriteLine("Press any key to continue");

                    //Console.ReadKey();

                    iAmount++;

                }



                 if (iGuess == iNum2)

                {

                    Console.WriteLine("you won $" + iMATCHTWO);

                    Console.WriteLine();

                    //Console.WriteLine("Press any key to continue");

                    //Console.ReadKey();

                    iAmount++;

                }


                 if (iGuess == iNum3)

                {

                    Console.WriteLine("you won $" + iMATCHTHREE);

                    Console.WriteLine();

                    //Console.WriteLine("Press any key to continue");

                    //Console.ReadKey();

                    iAmount++;

                }


                 if (iGuess == iNum1 && iGuess == iNum2 && iGuess == iNum3)

                {

                    Console.WriteLine("you won $" + iMATCHFOUR);

                    Console.WriteLine();

                    //Console.WriteLine("Press any key to continue");

                    //Console.ReadKey();

                    iAmount++;

                }


                Console.WriteLine("you have matched " + iAmount + " numbers");

            }


            Console.WriteLine("The numbers are " + iNum1 + ", " + iNum2 + ", " + iNum3 + ", ");

            Console.ReadKey();

        }

    }

}


AuthorReply
Vulpes
  • 1
  • 0
accepted
Re: Create a console-based lottery game application - Help needed by anyone
Posted on: 10 Aug 2012   Accepted Answer
I've had to change it quite a bit but try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace random
{

    class Program
    {

        static void Main(string[] args)
        {
            // declaring integers

            int iNum1;
            int iNum2;
            int iNum3;
            
            int iCount;
            int iGuess;

            // declaring constants

            const int iBALLS = 3;
            const int iMATCHONE = 10;
            const int iMATCHTWO = 100;
            const int iMATCHTHREE = 1000;
            const int iMATCHFOUR = 10000;


            // declaring arrays

            int[] iSortedNums = new int[3];
            int[] iGuesses = new int[3]; 
            int[] iSortedGuesses = new int[3];  

            // generating random numbers

            Random randomnumber = new Random();

            iNum1 = randomnumber.Next(1, 5); // excludes endpoint so gives number between 1 and 4
            iNum2 = randomnumber.Next(1, 5);
            iNum3 = randomnumber.Next(1, 5);
            iSortedNums[0] = iNum1;
            iSortedNums[1] = iNum2;
            iSortedNums[2] = iNum3;
            Array.Sort(iSortedNums); // sort random numbers

            for (iCount = 0; iCount < iBALLS; iCount++)
            {
                Console.Write("\nNo:" + (iCount + 1) + " Choose your number: ");
                iGuess = Convert.ToInt32(Console.ReadLine());
    
                while (iGuess < 1 || iGuess > 4)
                {
                    Console.WriteLine("You have entered an incorrect number please try again");
                    Console.Write("\nNo:" + (iCount + 1) + " Choose your number: ");
                    iGuess = Convert.ToInt32(Console.ReadLine());
                }

                iGuesses[iCount] = iGuess;
                iSortedGuesses[iCount] = iGuess;
            }

            Array.Sort(iSortedGuesses); // sort the guesses             

            Console.WriteLine();
            Console.WriteLine("The random numbers are    : " + iNum1 + ", " + iNum2 + ", " + iNum3);
            Console.WriteLine("The numbers you chose are : " + iGuesses[0] + ", " + iGuesses[1] + ", 

" + iGuesses[2]);
            Console.WriteLine();

            if (iGuesses[0] == iNum1 && iGuesses[1] == iNum2 && iGuesses[2] == iNum3)
            {
                 Console.WriteLine("you won $" + iMATCHFOUR);
            }

            else if(iSortedGuesses[0] == iSortedNums[0] && iSortedGuesses[1] == iSortedNums[1]
            && iSortedGuesses[2] == iSortedNums[2]) 
            {
                 Console.WriteLine("you won $" + iMATCHTHREE);
            }

            else if((iGuesses[0] == iNum1 && iGuesses[1] == iNum2) || ( iGuesses[1] == iNum2 && 

iGuesses[2] == iNum3))
            {
                 Console.WriteLine("you won $" + iMATCHTWO);
            }

            else if (iGuesses[0] == iNum1 || iGuesses[1] == iNum2 || iGuesses[2] == iNum3)           
            {
                 Console.WriteLine("you won $" + iMATCHONE);
            }
            
            else
            {
                Console.WriteLine("sorry, you didn't win anything");
            }

            Console.ReadKey();
        }

    }

}



Re: Create a console-based lottery game application - Help needed by anyone
Posted on: 10 Aug 2012  
Really thanks  Vulpes i like you .... u have a great programing knowledge ...now am working on GUI part of this program i hope i will do it with the help of modifying code...again thanks for help

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.