CODE!!!

Published August 01, 2007
Advertisement
I finally finished something. I didn't implement it in the way I wanted to, but close enough.

Here is Guess My Number

Post in Beginners Forum
Thread for my code!

using System;namespace GuessMyNumber{    class GuessMyNumber    {        static void Main()        {            bool playingGame = true;            Console.WriteLine("Welcome to Guess My Number!  Do you think you can guess my number?");            while (playingGame)            {                PlayGame();                Console.Write("Would you like to play again? ");                bool asking;                string playAgain;                asking = true;                while (asking)                {                    playAgain = Console.ReadLine();                    playAgain = playAgain.ToLower();                    if (playAgain == "n" || playAgain == "no")                    {                        playingGame = false;                        asking = false;                    }                    else if (playAgain == "y" || playAgain == "yes")                    {                        playingGame = true;                        asking = false;                    }                    else                        Console.Write("I do not understand your selection.  Please type yes or no: ");                }            }        }        static int PickRandomNumber()        {            int minNumber = 1;            int maxNumber = 100;            Random pickNumber = new Random();            return pickNumber.Next(minNumber, maxNumber);        }        static void PlayGame()        {            int randomNumber;            int userInput = 0;            int numberOfTries = 0;            bool guessing = true;            bool gettingInput;            randomNumber = PickRandomNumber();            while (guessing)            {                gettingInput = false;                while (gettingInput == false)                {                    Console.Write("Please pick a number between 1 and 100: ");                    gettingInput = int.TryParse(Console.ReadLine(), out userInput);                }                if (gettingInput)                {                    if (userInput > 1 && userInput < 100)                    {                        numberOfTries += 1;                        if (userInput == randomNumber)                        {                            Console.WriteLine("Good Job!\n");                            guessing = false;                        }                        else if (userInput < randomNumber)                            Console.WriteLine("Try guessing a higher number!\n");                        else if (userInput > randomNumber)                            Console.WriteLine("Try guessing a lower number!\n");                    }                }            }            Console.WriteLine("You guessed {0} times!\n", numberOfTries);        }    }}


Let me know how I did!
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Not much to update

881 views

Can't Sleep

854 views

School Blows

753 views

A new post! OMG!

833 views

CODE!!!

889 views

First Post!

770 views
Advertisement