Jump to content

Questions about methods in C#

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi everyone,

I have recently switched from Java to C# due to their similarity. I have made a guessing game console application for C#. The computer picks a random number between 1 - 10 and the user has three attempts to guess the number.


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

namespace ConsoleApplication1
{

    class WinnerCloser
    {


        public WinnerCloser()
        {

            int x = 0;

            x++;

            if ((x > 0))

                Console.WriteLine("\r\nPress any key to exit...");

            Console.ReadKey();

            Environment.Exit(0); 

        }

    }

    class LoserCloser
    {

        public LoserCloser()
        {

            Console.WriteLine("\r\nSorry, that's three guesses!");

        }

    }

 



    class Program
    {




        static void Main(string[] args)
        {




            Random r = new Random();

            int pickNum = r.Next(11);

            Console.WriteLine("Welcome to The Number Game!");

            Console.WriteLine("\r\nThe purpose of this game is to guess the number the computer has picked.");

            Console.WriteLine("The number is between 1 and 10. You will have three attempts to guess\r\nthe right number. Good luck!");

            Console.WriteLine("\r\nEnter your first guess:");

            Console.WriteLine("\r\n");

            String firstGuess = Console.ReadLine();

            int guess1 = Int32.Parse(firstGuess);

            if ((guess1 == pickNum))
            {

                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");

                new WinnerCloser();



            }

            else



                Console.WriteLine("\r\nSorry, guess again!");

            Console.WriteLine("\r\nEnter your second guess:");

            Console.WriteLine("\r\n");

            String secondGuess = Console.ReadLine();

            int guess2 = Int32.Parse(secondGuess);

            if ((guess2 == pickNum))
            {




                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");

                new WinnerCloser();

            }




            else



                Console.WriteLine("\r\nSorry, guess again!");

            Console.WriteLine("\r\nEnter your third guess:");

            Console.WriteLine("\r\n");

            String thirdGuess = Console.ReadLine();

            int guess3 = Int32.Parse(thirdGuess);

            if ((guess3 == pickNum))
            {




                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");

                new WinnerCloser();

            }




            else
            {

                new LoserCloser();

                Console.WriteLine("\r\nThe number was " + pickNum + "!");

                Console.WriteLine("\r\nPress any key to exit..");

                Console.ReadKey();

            

                

            }

        }

    }

}

I am creating the classes WinnerCloser and LoserCloser because I cannot get them to be methods elsewhere. This is the only way I have been able to have them in the program and use them without the compiler giving me an error. I tried declaring the methods before the class program, but that didn't work either. Suggestions would be appreciated.

Thanks in advanced :thumbup1:

#2
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Instead of having WinnerCloser and LoserCloser in their own classes, what you can do is make them methods inside the Program class. You have to put this code inside the class, but outside the main method.


public static void LoserCloser()

{

     Console.WriteLine("\r\nSorry, that's three guesses!");

}


public static void WinnerCloser()

{

            int x = 0;

            x++;

            if ((x > 0))

                Console.WriteLine("\r\nPress any key to exit...");


            Console.ReadKey();

            Environment.Exit(0);

}


Now the only thing I did to edit the actual code was add static and void onto these methods. Use the static keyword when you know this method won't be used outside it's class. I also assigned this method a void return type because you do not want it to return any value, you just want it to write something to the console. Another thing to note, since you don't need to create an object, just remove the new keyword before the method.

In case you are confused about where the code should go, here is what it would look like.


class Program

    {

        static void Main(string[] args)

        {

            Random r = new Random();


            int pickNum = r.Next(1, 11);


            Console.WriteLine("Welcome to The Number Game!");


            Console.WriteLine("\r\nThe purpose of this game is to guess the number the computer has picked.");


            Console.WriteLine("The number is between 1 and 10. You will have three attempts to guess\r\nthe right number. Good luck!");


            Console.WriteLine("\r\nEnter your first guess:");


            Console.WriteLine("\r\n");


            String firstGuess = Console.ReadLine();


            int guess1 = Int32.Parse(firstGuess);


            if ((guess1 == pickNum))

            {


                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");


                WinnerCloser();

            }


            else


            Console.WriteLine("\r\nSorry, guess again!");


            Console.WriteLine("\r\nEnter your second guess:");


            Console.WriteLine("\r\n");



            String secondGuess = Console.ReadLine();


            int guess2 = Int32.Parse(secondGuess);



            if ((guess2 == pickNum))

            {

                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");

                WinnerCloser();


            }


            else




                Console.WriteLine("\r\nSorry, guess again!");


            Console.WriteLine("\r\nEnter your third guess:");


            Console.WriteLine("\r\n");


            String thirdGuess = Console.ReadLine();


            int guess3 = Int32.Parse(thirdGuess);


            if ((guess3 == pickNum))

            {

                Console.WriteLine("\r\nCongratulations! You've guessed the number correctly!");

                WinnerCloser();

            }





            else

            {

                LoserCloser();


                Console.WriteLine("\r\nThe number was " + pickNum + "!");


                Console.WriteLine("\r\nPress any key to exit..");


                Console.ReadKey();

            }


        }


        public static void WinnerCloser()

        {


            int x = 0;


            x++;


            if ((x > 0))


                Console.WriteLine("\r\nPress any key to exit...");


            Console.ReadKey();


            Environment.Exit(0);

        }


        public static void LoserCloser()

        {


            Console.WriteLine("\r\nSorry, that's three guesses!");


        }


    }


Also, one last thing. Your randomization made it possible to have 0 as a value. Random.Next() also has a method to set a minimum value. Notice how I used r.Next(1,11) to make the minimum value 1, and the upper bound 11.

#3
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Thanks for the response. I must have been doing something wrong when I tried to make the methods. However, I have another question.

In the main section of my program I create the integer pickNum. The value of pickNum will be displayed at the end of the game if the user did not guess correctly. I would like to have everything that is supposed to happen when the game is over to occur in the LoserCloser method. However, if I add the code

Console.WriteLine("\r\nThe number was " + pickNum + "!");


             Console.WriteLine("\r\nPress any key to exit..");


             Console.ReadKey();

to the LoserCloser method I get the error

Quote

The name 'pickNum' does not exist in the current context
which I believe is happening because pickNum is not initialized until after the main method. So I decided to make a new method called genNum, which will pick the number and be called after the main method. However, I receive the error

Quote

The name 'pickNum' does not exist in the current context
wherever pickNum is referenced. genNum is the first method, and pickNum is initialized in it :confused:.

#4
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Ok, your next problem is very simple to solve.

All you have to do is declare pickNum as a static int inside the program class, and outside the main method. Once that is done, you can just assign it inside the main method, the rest of the code is fine as it is. Here is an example of what I mean:

class program
{
     static int pickNum;

     static void Main()
     {
          Random r = new Random();
          pickNum = r.Next(1,11);
     }
}

That is all you need to change.

#5
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Thanks. That seems to have made everything work how I wanted it to. Appreciate it! :thumbup:

#6
warden26

warden26

    Newbie

  • Members
  • PipPip
  • 26 posts
C sharp is a great language, and it shouldn't be hard to switch after java. For me, both are great. I use java at home, and c sharp at work, and transition has been easy for me.

Thanks

Edited by warden26, 31 August 2010 - 05:33 AM.

It's the worlds hardest game, what can I say more?

http://www.worlds-hardest-game.com