-1

だから私は宝くじを作っています。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();   
        }
    }
}

問題は、プログラムが停止せずに進み続けることです。ラッキーナンバーと宝くじの数字が同じになった時点で停止するはずです。

4

2 に答える 2

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();


        }
    }
于 2012-12-16T17:53:40.957 に答える
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;
        }
    }
}

ここに画像の説明を入力 お役に立てば幸いです。

于 2012-12-16T17:57:31.980 に答える