Created
October 20, 2013 19:52
-
-
Save hagbarddenstore/7074441 to your computer and use it in GitHub Desktop.
A guess the number game!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace E | |
| { | |
| using System; | |
| public static class Program | |
| { | |
| public static void Main() | |
| { | |
| var random = new Random(); | |
| var response = string.Empty; | |
| Console.WriteLine("Welcome to the number guessing game!"); | |
| do | |
| { | |
| Console.WriteLine("A random number will be chosen between 1 and 10"); | |
| var number = random.Next(1, 11); | |
| var guess = 0; | |
| var numberOfGuesses = 0; | |
| do | |
| { | |
| Console.WriteLine("Please guess a number: "); | |
| response = Console.ReadLine(); | |
| if (!int.TryParse(response, out guess)) | |
| { | |
| Console.WriteLine("Not a valid number! \"{0}\"", response); | |
| continue; | |
| } | |
| numberOfGuesses += 1; | |
| if (guess > number) | |
| { | |
| Console.WriteLine("Lower..."); | |
| } | |
| else if (guess < number) | |
| { | |
| Console.WriteLine("Higher..."); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Well done! You guessed correctly!"); | |
| if (numberOfGuesses < 4) | |
| { | |
| Console.WriteLine("You're an expert!"); | |
| } | |
| else if (numberOfGuesses < 7) | |
| { | |
| Console.WriteLine("You're pretty good!"); | |
| } | |
| else if (numberOfGuesses < 10) | |
| { | |
| Console.WriteLine("You're about average!"); | |
| } | |
| else | |
| { | |
| Console.WriteLine("You're lousy!"); | |
| } | |
| } | |
| } | |
| while (guess != number); | |
| Console.Write("Would you like to play another turn? (y/n): "); | |
| response = Console.ReadLine(); | |
| } | |
| while ("y".Equals(response, StringComparison.InvariantCultureIgnoreCase)); | |
| Console.WriteLine("Thanks for playing!"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment