Advertisement
C_Sharp_HUB

A C# Windows Forms Number Guessing Game

Mar 25th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | Source Code | 0 0
  1. namespace NumberGuessing
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void btnGo_Click(object sender, EventArgs e)
  11.         {
  12.             Random rnd = new Random();
  13.             int randomNumber = rnd.Next(1, 10);
  14.             int theirNumber = Convert.ToInt32(txtGuess.Text);
  15.             string outcome = "";
  16.  
  17.             if (randomNumber == theirNumber)
  18.             {
  19.                 outcome = "Congratulations! \n You guessed correctly. \n I also thought of: " + randomNumber;
  20.             }
  21.             else if (randomNumber > theirNumber)
  22.             {
  23.                 outcome = "Your guess is too low. \n I thought of: " + randomNumber;
  24.             }
  25.             else
  26.             {
  27.                 outcome = "Your guess is too high. \n I thought of: " + randomNumber;
  28.             }
  29.             lblResult.Text = outcome;
  30.             txtGuess.Text = "";
  31.             txtGuess.Focus();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement