乐透计划不会停止

时间:2012-12-16 16:50:41

标签: c#

所以我正在做很多游戏。您必须输入6个幸运数字,如果它们与乐透号码相同,那么您就赢了。

这是我的代码:

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

namespace OefeningExaam
{
    class Program
    {
        static void Main(string[] args)
        {
            Random getal = new Random();
            int[] lottotrekking = new int[6];

            Console.WriteLine("Geef je geluksgetallen in <tussen 1 en 42>");
            Console.WriteLine("Geef je eerste getal in");
            int getal1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je tweede getal in");
            int getal2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je derde getal in");
            int getal3 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vierde getal in");
            int getal4 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vijfde getal in");
            int getal5 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je zesde getal in");
            int getal6 = Convert.ToInt32(Console.ReadLine());

            while (getal1 != lottotrekking[0] || getal2 != lottotrekking[1] || getal3 != lottotrekking[2] || getal4 != lottotrekking[3] || getal5 != lottotrekking[4] || getal5 != lottotrekking[4] || getal6 != lottotrekking[5])
            {

                for (int i = 0; i < lottotrekking.Length; i++)
                {
                    int cijfer = getal.Next(1, 43);
                    lottotrekking[i] = cijfer;
                    Console.WriteLine(lottotrekking[0] + "\t " + lottotrekking[1] + "\t " + lottotrekking[2] + "\t " + lottotrekking[3] + "\t " + lottotrekking[4] + "\t " + lottotrekking[5]);
                }
            }

            if (getal1 == lottotrekking[0] && getal2 == lottotrekking[1] && getal3 == lottotrekking[2] && getal4 == lottotrekking[3] && getal5 == lottotrekking[4] && getal5 == lottotrekking[4] && getal6 == lottotrekking[5])
            {
                Console.WriteLine(lottotrekking[0] + " " + lottotrekking[1] + " " + lottotrekking[2] + " " + lottotrekking[3] + " " + lottotrekking[4] + " " + lottotrekking[5]);
            }

            Console.ReadLine();   
        }
    }
}

问题在于程序只是继续运行并且不会停止。当幸运数字与乐透号码相同时,它应该停止。

2 个答案:

答案 0 :(得分:0)

这样的事情会起作用。但是匹配所有数字可能需要一段时间。

static void Main(string[] args)
        {
            Random getal = new Random();
            int[] lottotrekking = new int[6];

            var getals = new List<int>();

            Console.WriteLine("Geef je geluksgetallen in <tussen 1 en 42>");
            Console.WriteLine("Geef je eerste getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Geef je tweede getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Geef je derde getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Geef je vierde getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Geef je vijfde getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Geef je zesde getal in");
            getals.Add(Convert.ToInt32(Console.ReadLine()));


            bool match = false;
            while (!match)
            {
                //Reset the array
                lottotrekking = new int[6];
                for (int i = 0; i < lottotrekking.Length; i++)
                {

                    //Make sure we dont add one number several times
                    int cijfer = 0;
                    while (lottotrekking.Any(x => x == cijfer) || cijfer == 0)
                    {
                        cijfer = getal.Next(1, 43);
                    }
                    lottotrekking[i] = cijfer;

                }
                Console.WriteLine(lottotrekking[0] + "\t " + lottotrekking[1] + "\t " + lottotrekking[2] + "\t " + lottotrekking[3] + "\t " + lottotrekking[4] + "\t " + lottotrekking[5]);

                match = lottotrekking.All(getals.Contains);

            }

            Console.WriteLine(lottotrekking[0] + " " + lottotrekking[1] + " " + lottotrekking[2] + " " + lottotrekking[3] + " " + lottotrekking[4] + " " + lottotrekking[5]);


            Console.ReadLine();


        }
    }

答案 1 :(得分:0)

这会产生随机数,直到它与用户输入相匹配,猜测正确的数字大约需要2分钟。

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

namespace OefeningExaam
{
    class Program
    {
        static void Main(string[] args)
        {
            Random getal              = new Random();
            int[]  lottotrekking      = new int[6];
            int[]  userInput          = new int[6];
            bool   isLotteryWon       = false;
            int    numberOfIterations = 0;

            Console.WriteLine("Geef je geluksgetallen in <tussen 1 en 42>");
            Console.WriteLine("Geef je eerste getal in");
            userInput[0] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je tweede getal in");
            userInput[1] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je derde getal in");
            userInput[2] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vierde getal in");
            userInput[3] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je vijfde getal in");
            userInput[4] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Geef je zesde getal in");
            userInput[5] = Convert.ToInt32(Console.ReadLine());

            while (!isLotteryWon)
            {
                isLotteryWon = true;

                // Generate the lottery numbers
                for (int i = 0; i < lottotrekking.Length; i++)
                {
                    int newNumber = getal.Next(1, 43);

                    while (Program.IsContained(lottotrekking, newNumber))
                        newNumber = getal.Next(1, 43);

                    lottotrekking[i] = newNumber;
                }


                Console.WriteLine(lottotrekking[0] + "\t " + lottotrekking[1] + "\t " + lottotrekking[2] + "\t " + lottotrekking[3] + "\t " + lottotrekking[4] + "\t " + lottotrekking[5]);

                //Compare lottery numbers to user input, if they match and are in the same order, the user wins.
                for (int i = 0; i < lottotrekking.Length; i++)
                    isLotteryWon &= Program.IsContained(lottotrekking, userInput[i]);

                numberOfIterations++;
            }

            Console.WriteLine(string.Format("Lottery won in {0} iterations", numberOfIterations));
            Console.ReadLine();
        }

        /// <summary>
        /// Determines whether the specified number is contained in the collection.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="number">The number.</param>
        /// <returns>
        ///   <c>true</c> if the specified number is contained in the collection; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsContained(int[] collection, int number)
        {
            for (int i = 0; i < collection.Length; i++)
            {
                if (collection[i] == number)
                    return true;
            }
            return false;
        }
    }
}

enter image description here 我希望它有所帮助。