-1

I've spent nearly an hour on this error and I can't seem to find where I went wrong (fresh set of eyes would probably help lol). it's the {array = Random.genRandom();} I'm also getting The name rand doesn't exist in the context for my MainClass. where it says ' and also getting an error for my contructor I'm basically just making a random number generator game where the number is displayed at the beginning and the person can get a strike, ball, etc.. for wrong answers. I have two classes. A Main class and a sub-class.

MainClass:

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

namespace NumBaseBall
{
    class MainClass
    {
        static void Main(string[] args)
        {
            int[] player;
            int[] array;
            int NumberOfTries = 0;
            int Strikes = 0;
            int ball = 0;
            int input;
            int count = 0;
            int g = 0;
            decimal game = 0;
            decimal wins = 0;
            decimal percent = 0;
            int Ru = 0;



            Console.WriteLine("Welcome to Number Baseball Game");
            Console.WriteLine("By: ");


            while (Ru != -1)
            {
                Console.WriteLine("The three Digit random number is: \t");

                MethodClass = rand = new MethodClass();

                array = Random.genRandom();
                Console.WriteLine();

                NumberOfTries = 0;

                game++;

                while (NumberOfTries < 5)
                {

                    ball = 0;
                    Strikes = 0;

                    Console.Write("Enter three digit number: \t");
                    input = Convert.ToInt32(Console.ReadLine());

                    player = MethodClass.splitNumber(input);

                    for (int i = 0; i < 3; i++)
                    {
                        count = 0;

                        if ((player[i] == array[g]) && (count == 0))
                        {
                            if (player[0] == array[0])
                            {
                                Strikes++;
                            }

                            else
                            {
                                ball++;
                            }
                            count++;

                        }

                        else if ((player[i] == array[g + 1]) && (Strikes == 0))
                        {
                            if (player[2] == array[2])
                            {
                                Strikes++;
                            }
                            else
                            {
                                ball++;
                            }
                        }
                        if (Strikes == 3)
                        {
                            Console.WriteLine("Goodjob! You guessed the random number!");
                            wins++;
                            continue;

                        }
                        else if (Strikes == 0 && ball == 0)
                        {
                            Console.WriteLine("0 Strikes, 0 ball");
                        }
                        else if ((Strikes > 0 && Strikes < 3) || (ball > 0))
                        {
                            Console.WriteLine("{0} Strikes and {1}", Strikes, ball);
                        }
                        NumberOfTries++;
                    }
                    if (Strikes == 0)
                    {
                        Console.WriteLine("\nYou lost!");
                    }
                    percent = (wins / game) * 100;

                    Console.WriteLine("Total amount played: {0}, Number of wins: {1}, Winning Percentage: {2:F2}",
                              game, wins, percent);
                    Console.WriteLine("Press Enter to exist");
                    Console.ReadLine();
                }
            }

        }
    }
}

MethodsClass:

//Mohamed Shire



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

namespace NumBaseBall
{
    class MethodClass
    {
        int[] random = new int[3];


        public int[] genRandom()
        {
            int my;
            Random rand = new Random();     
            for (int i = 0; i < random.Length; i++)
            {
                my = rand.Next(1, 10);
                if (isExists(random, my) == false)
                {
                    random[i] = my;
                    Console.Write(random[i]);
                }
                else
                {
                    i--;
                }
            }
            return random;


        }

        public bool isExists(int[] array, int my)
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == my)
                {
                    return true;

                }
            }

            return false;
        }

        public static int[] splitNumber(int input)
        {
            int[] uInput = new int[3];
            uInput[0] = input / 100;
            uInput[1] = input % 100 / 10;
            uInput[2] = input % 100 % 10;
            return uInput;

        }

        public static void outputArray(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }

        }


    }


}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Please read [this](https://stackoverflow.com/help/mcve) and try trimming your code to a smaller example that reproduces the issue. In doing so you'll likely solve the problem for yourself. – Preston Guillot Feb 14 '16 at 00:53
  • Use [`int.TryParse()`](http://stackoverflow.com/a/199484/380384) to ensure smooth flow of the program, after you check for `ENTER`. – John Alexiou Feb 14 '16 at 01:19

1 Answers1

0

Try changing this:

MethodClass = rand = new MethodClass();

array = Random.genRandom();

to:

MethodClass  rand = new MethodClass();

array = rand.genRandom();
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Hey just a quick question: I want a prompt at the end where when a player hits 0 the game restarts and when they hit enter, it terminates. Here's what I have: `Console.Write("To continue playing, hit 0: To exit the game hit enter. To continue enter any number other than -1 : ");` – JavaStudent22 Feb 14 '16 at 01:06
  • I know I can use the `Convert.ToInt32` for the 0 but what about when they hit ENTER to exit? – JavaStudent22 Feb 14 '16 at 01:08