Advertisement
SteelGolem

pick a number between 1 and 20

Jun 18th, 2020
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1.  
  2. using System;
  3.  
  4. namespace ConsoleApplication1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Random rand = new Random();
  11.             int number = rand.Next(20) + 1;
  12.             Console.WriteLine("i've picked a number from 1 to 20. guess it!");
  13.             int guess = 0;
  14.             while (guess != number)
  15.             {
  16.                 Console.Write("guess: ");
  17.                 string line = Console.ReadLine();
  18.                 line = line.Trim();
  19.                 bool validGuess = true;
  20.                 try { guess = int.Parse(line); }
  21.                 catch { validGuess = false; }
  22.                 if (validGuess)
  23.                 {
  24.                     if (guess < 1 || guess > 20) Console.WriteLine("that wasn't even between 1 and 20!");
  25.                     else if (guess != number) Console.WriteLine("that's not the number!");
  26.                     else Console.WriteLine("you're right! it was " + number + "!");
  27.                 }
  28.                 else Console.WriteLine("that wasn't a number.");
  29.             }
  30.             Console.WriteLine("good job, you won!");
  31.             Console.ReadKey();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement