Create the higher/lower guessing game using a graphical user interface.Allow use
ID: 3645003 • Letter: C
Question
Create the higher/lower guessing game using a graphical user interface.Allow users tokeep guessing until they guess the number. Choose two colors for your game: one
should be used to indicate that the value the users guessed is higher than the target; the
other is used to indicate that the value the users guessed is lower than the target.With
each new guess, change the form color based on whether the guess is higher than the
target or lower.Keep a count of the number of guesses.When they hit the target, display
a MessageBox indicating the number of guesses it took. Several approaches can
be used to seed the target: one is to generate a random number by constructing an
object of the Random class. For example, the following stores a random whole
number between 0 and 100 in target:
Random r=new Random();
int target=r.Next(0,100);
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DaniwebHelp1
{
public partial class Form1 : Form
{
int range;
public Form1()
{
InitializeComponent();
//initialize the random number generator
Random num = new Random();
//set random number parameters
range = num.Next(0, 100);
}
private void button1_Click(object sender, EventArgs e)
{
//initialize variables
int guess;
guess = int.Parse(textBox1.Text);
//parameters to show higher or lower
if (guess > range)
{
this.BackColor = Color.Red;
}
else if (guess < range)
{
this.BackColor = Color.Green;
}
else
{
MessageBox.Show("YOU WIN!");
}
}
}
}