0

I'm creating a lottery program that users can choose from a series of numbers between a particular range and the program then creates its own version of numbers and compares those user values against the program's random numbers which any matches that the program then finds are then displayed to the user.

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

namespace Assessment1.Lottery
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("Please insert 6 lottery numbers:");
        int range = 100;
        int minValue = 0;
        int maxValue = 100;
        int a = 0;

        Random rnd = new Random();

        int[] myArray = new int[6];
        
        for ( int i=0; i < myArray.Length; i++ )
        {
            int randomNumber = rnd.Next(minValue, maxValue);
            myArray[i] = randomNumber;
            Console.WriteLine(randomNumber);
            myArray[i] = int.Parse(Console.ReadLine());
                            Console.WriteLine(myArray[i]);

            while (a < 5)
        {
                if (int.TryParse(Console.ReadLine(), out myArray[a]))
                    a++;
                else 
                    Console.WriteLine("invalid integer");
        }
        double arry = myArray.Average();
            if(arry > 0)
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");

        int BinarySearch(int[] array, int value, int low, int high)
        {
            if ( high >= low)
            {
                int mid = (low + high) / 2;
                if (array[mid] == value) return mid;
                if (array[mid] == value) return BinarySearch(array, value, low, mid - 1);
                return BinarySearch(array, value, mid + 1, high);

            }
            return -1;
        }


        }

       Console.ReadLine();
        }
    }
}

when I run the program it shows the following

Please insert 6 lottery numbers: 64

how can I fix this so that the array can be filled with user input

1
  • 2
    At least one problem is that you never fill myArray with anything, it is initialized with 6 items, all with the value 0. Please read How to debug small programs Commented Mar 24, 2022 at 13:01

1 Answer 1

1

Here is a version of something I wrote a while back and modified to your parameters, if there are bugs please don't ask me to fix.

 static void Main(string[] args)
            {
                int stop = 0;
                int[] chosenNum = new int[6];
                while (stop == 0)
                {
                    Console.Clear();
                    string con = "";
                    Console.WriteLine("Enter six numbers between 1-100 separated by a comma with no duplicates:");
                    string numbers = Console.ReadLine();
                    string[] userNumbers = numbers.Replace(" ", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToArray();;//remove ex: ,, issues
                    string[] checkDup = userNumbers.Distinct().ToArray();//remove duplicates
                    if (userNumbers.Count() < 6)
                    {
                        Console.WriteLine("You entered less than six numbers");
                        Console.WriteLine("Try Again Y or N");
                        con = Console.ReadLine();
                        if (con.ToUpper() != "Y")
                        {
                            stop = 1;
                        }
                    }
                    else if (userNumbers.Count() > 6)
                    {
                        Console.WriteLine("You entered more than 6 numbers");
                        Console.WriteLine("Try Again Y or N");
                        con = Console.ReadLine();
                        if (con.ToUpper() != "Y")
                        {
                            stop = 1;
                        }
                    }
                    else if (checkDup.Count() < 6)
                    {
                        Console.WriteLine("You entered duplicate numbers");
                        Console.WriteLine("Try Again Y or N");
                        con = Console.ReadLine();
                        if (con.ToUpper() != "Y")
                        {
                            stop = 1;
                        }
                    }
                    else if (!isNumeric(userNumbers))
                    {
                        Console.WriteLine("You entered non-numeric value(s)");
                        Console.WriteLine("Try Again Y or N");
                        con = Console.ReadLine();
                        if (con.ToUpper() != "Y")
                        {
                            stop = 1;
                        }
                    }
                    else if (isInRange(userNumbers) < 6)
                    {
                        Console.WriteLine("You entered out of range value(s)");
                        Console.WriteLine("Try Again Y or N");
                        con = Console.ReadLine();
                        if (con.ToUpper() != "Y")
                        {
                            stop = 1;
                        }
                    }
                    else
                    {
                        var lowerBound = 1;
                        var upperBound = 100;
                        var random = new Random();
                        int[] randomNum = new int[6];
                        int count = 0;
                        foreach(string str in userNumbers){
                            var rNum = random.Next(lowerBound, upperBound);
                            randomNum[count] = rNum;
                            count++;
                            
                        }
                        
                        string[] ourPicks = Array.ConvertAll(randomNum, s => s.ToString()).ToArray();
                        Array.Sort(userNumbers);
                        Array.Sort(ourPicks);
                        //string[] ourpicks = { "1", "2" };//for testing
                        Console.WriteLine("Your Numbers: {0}", string.Join(", ", userNumbers));
                        Console.WriteLine("Our Numbers: {0}", string.Join(", ", ourPicks));
                        string[] result = userNumbers.Intersect(ourPicks).ToArray();
                        if(result.Count() > 0)
                        {
                            Console.WriteLine("Matchs: {0}", string.Join(", ", result));
                        }
                        else
                        {
                            Console.WriteLine("Match's = 0");
                        }
                        stop = 1;
                    }
    
                    
    
                }
    
                Console.ReadLine();
    
    
    
    
    
            }
    
            public static bool isNumeric(string[] num)
            {
    
                foreach (string str in num)
                {
                    bool check = int.TryParse(str, out int test);
    
                    if (!check)
                    {
                        return false;
                    }
                }
                return true;
    
            }
    
            public static int isInRange(string[] num)
            {
                int count = 0;
                foreach (string str in num)
                {
                    int.TryParse(str, out int test);
    
                    if (test > 0 & test <= 100)
                    {
                        count++;
                    }
                }
                return count;
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.