Home > Net >  Why are these while loops not working? Or is something else the problem?
Why are these while loops not working? Or is something else the problem?

Time:10-04

i have just recently started c# as this language is being used for the course at college. [The top prize in a lottery is won by matching three numbers between 1 and 30 to three random numbers drawn in the same order. When a ball is drawn it is put back into the machine before another ball is drawn. There are always 30 balls in the machine before a ball is drawn and a player may choose the same ball more than once. A draw takes place once a week. Write an algorithm that takes three numbers as inputs, repeatedly draws three random numbers between 1 and 30 until there are three matches and returns the number of weeks it took to win the jackpot.-- This is the program i have to create. Is it any better without the picture. When i execute this code, it runs infinitely and doesn't give me a specific answer.

][1]

using System;

namespace challenges { class Program { static void Main(string[] args) { // Lottery Problem

        double num1, num2, num3;
        int week = 0;

        Console.Write("Enter the first number: ");
        num1 = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the second number: ");
        num2 = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the third number: ");
        num3 = Convert.ToDouble(Console.ReadLine());

        Random rnd = new Random();
        int ball1 = rnd.Next(1, 31);
        int ball2 = rnd.Next(1, 31);
        int ball3 = rnd.Next(1, 31);

        do
        {
            week  ;

            do
            {
                Console.WriteLine("it took "   week   " weeks to win the jackpot");
                break;
            } while (ball1 == num1 && ball2 == num2 && ball3 == num3); 
        } while (ball1 != num1 || ball2 != num2 || ball3 != num3);

CodePudding user response:

The problem is that you have to pass random selection codes into the loop.I mean "that for line that select random numbers".

  • Related