Home > Back-end >  **SOLVED ** Switch not calling method in c# **CORRET CODE POSTED BELOW**
**SOLVED ** Switch not calling method in c# **CORRET CODE POSTED BELOW**

Time:12-20

Apologies for the quetion, I am quite new to this. I have misjudged the whole problem.

I am trying to create a lottery program. There are 2 lotteries, the aim is for the user to select which lottery they wish to take part in. I have a method for either lottery which work independently.

I am trying to use the main method to call the respective methods but do not know how to do so.

I have tried to use a switch for the user to select which lottery. But the methods are not being called. Receiving CS7036 error in the main and CS0161 in each method

If there was any advice on how to write the main method to call the other methods it would be appreciated.

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

namespace LotteryAttemptOne
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int choice = 0;
            do
            {
                DisplayMenu();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        LottoDraw();
                        break;

                    case 2:
                        EuroDraw();
                        break;

                    default:
                        Console.WriteLine("Please enter 1 for EuroMillions or 2 for Lotto");
                        break;
                }
            } while (choice != 2);
        }

        public static void LottoDraw()
        {
            int[] lotto = new int[6];
            Random LottoNumbers = new Random();

            for (int i = 0; i < lotto.Length; i  )
            {
                lotto[i] = LottoNumbers.Next(1, 47);
            }
            Array.Sort(lotto);
            Console.WriteLine("Your Lotto Numbers are:");
            for (int i = 0; i < lotto.Length; i  )
                Console.WriteLine(lotto[i]);
            Console.ReadLine();
        }

        public static void EuroDraw()
        {
            int[] EuroMillions = new int[5];
            Random EuroNumbers = new Random();

            for (int i = 0; i < EuroMillions.Length; i  )
            {
                EuroMillions[i] = EuroNumbers.Next(1, 50);
            }
            Array.Sort(EuroMillions);

            Console.WriteLine("Your Euro Millions Numbers are:");
            for (int i = 0; i < EuroMillions.Length; i  )
                Console.WriteLine(EuroMillions[i]);
            Console.ReadLine();
        }
        public static void DisplayMenu()
        {
            Console.WriteLine();
            Console.WriteLine("1: Select Lotto");
            Console.WriteLine("2: Select Euro Millions");
            Console.WriteLine();
            Console.Write("Please chose a Menu option (1 - 2) : ");
        }
    }
}

CodePudding user response:

Answer posted above also:

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

namespace LotteryAttemptOne
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int choice = 0;
            do
            {
                DisplayMenu();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        LottoDraw();
                        break;

                    case 2:
                        EuroDraw();
                        break;

                    default:
                        Console.WriteLine("Please enter 1 for EuroMillions or 2 for Lotto");
                        break;
                }
            } while (choice != 2);
        }

        public static void LottoDraw()
        {
            int[] lotto = new int[6];
            Random LottoNumbers = new Random();

            for (int i = 0; i < lotto.Length; i  )
            {
                lotto[i] = LottoNumbers.Next(1, 47);
            }
            Array.Sort(lotto);
            Console.WriteLine("Your Lotto Numbers are:");
            for (int i = 0; i < lotto.Length; i  )
                Console.WriteLine(lotto[i]);
            Console.ReadLine();
        }

        public static void EuroDraw()
        {
            int[] EuroMillions = new int[5];
            Random EuroNumbers = new Random();

            for (int i = 0; i < EuroMillions.Length; i  )
            {
                EuroMillions[i] = EuroNumbers.Next(1, 50);
            }
            Array.Sort(EuroMillions);

            Console.WriteLine("Your Euro Millions Numbers are:");
            for (int i = 0; i < EuroMillions.Length; i  )
                Console.WriteLine(EuroMillions[i]);
            Console.ReadLine();
        }
        public static void DisplayMenu()
        {
            Console.WriteLine();
            Console.WriteLine("1: Select Lotto");
            Console.WriteLine("2: Select Euro Millions");
            Console.WriteLine();
            Console.Write("Please chose a Menu option (1 - 2) : ");
        }
    }
}

CodePudding user response:

These are not calling LottoDraw(int[] arg) for example because they have a parameter in the signature and you pass none LottoDraw(); Now they expect an array of int so you need to determine what/how that is created OR perhaps just call with the one? LottoDraw(choice); and LottoDraw(int arg) SO how do you wish to create that int[] there?

  • Related