lundi 21 septembre 2020

c# randomNumber in array == winNumber

So i'm a beginner in coding, (1month). I have been changing the code for 4 days now, no luck. i'm stuck at getting my randomNumber == winNum to break. The user gets 10 numbers, if the user writes (lets say winNum is 9) 1,2,5,9,10,11.... and so on. The code wont pick the number in the array, and print the "you lucky". The code used to work. 9.2.3.4.5..... and so on, where it gets "you lucky" but not anymore. I also get "you lose""you lose""you lose" and "End of the game".

Unsure what i'm doing wrong.

    static void Main(string[] args)
    {
        // -int size = Convert.ToInt32(Console.In.ReadLine());   // this to change amount of variables the user wants instead of [x]
        int[] numbers = new int[10];  //  task 1, modul 3. The 10 in the array is a statment for how many varibles this array can hold.
                                     // you can have as many varibles as you want.
        Random randomNumbers = new Random();
        int winNum = randomNumbers.Next(1, 26);
        Console.WriteLine(winNum);
        Console.WriteLine("Pick your 10 numbers! "); // här får vi våra 10 tal av användaren 
            for (int hallon = 0; hallon < 10; hallon++)  //this loop will print the users numbers
            {
                
                Console.WriteLine($"Your have used: {hallon}" ); // här får vi våra 10 tal av användaren 
            try
            {
                numbers[hallon] = Convert.ToInt32(Console.In.ReadLine());  // numbers lagrar våra 10 tal som kommer spelas nu mot winNum
            }
            catch (SystemException e) // going to catch if the user writes in a letter instead of a number        
            {
                --hallon;
                Console.WriteLine(e.Message);
            }
            if (numbers[hallon] < 1 || numbers[hallon] > 25)
                {
                    Console.WriteLine("Write a number within 1-25. ");
                    --hallon;
                }
            } 
        for (int hallon = 0; hallon < 10; hallon++)
        {
            if (numbers[hallon] == winNum)  // vad gör jag om jag vill ha två vinnande resultat? 
            {
                Console.WriteLine("Lucky number:" + numbers[hallon]);
                break;
            } ////task 2 modul 3 this loops my array untill i have all varbiles printed 
        }

        for (int hallon = 0; hallon < 10; hallon++)
        {
            if (numbers[hallon] != winNum)
            Console.WriteLine("You lost");

        }
        Console.WriteLine("End of the game");
        Console.ReadLine();
    }
    }



Aucun commentaire:

Enregistrer un commentaire