Skip to content

Instantly share code, notes, and snippets.

@MrAwesomeness
Created June 2, 2015 21:10
Show Gist options
  • Save MrAwesomeness/6196354b6643faaad801 to your computer and use it in GitHub Desktop.
Save MrAwesomeness/6196354b6643faaad801 to your computer and use it in GitHub Desktop.
C# guess a number from 1 to 100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int guess = 0;
string welcome = "Guess a number between 1 and 100";
int num = rand.Next(1,100);
Console.WriteLine(welcome);
int i = 0;
while(guess != num)
{
try
{
guess = Convert.ToInt32(Console.ReadLine());
if (guess > num)
{
Console.WriteLine("Too High");
}
else
{
Console.WriteLine("Too Low");
}
}
catch
{
Console.WriteLine("Guess must be a number");
i--;
}
i++;
}
Console.WriteLine("Congrats, it took you " + i + " tries");
Console.ReadLine();
}
}
}
@holy-boi
Copy link

So awesome

@Glorious-MG
Copy link

COOL

@karlmagar
Copy link

it doesn't work at all, did you even try it before publishing ?

@DecentCoder1
Copy link

it doesn't work at all, did you even try it before publishing ?

just add 1 in the parenthesis of new Random()

@ismaehl-2002
Copy link

You can when user say exit it will say bye and guess game exit and final number will be shown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment