TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Lucian Negru
NA
3
1.3k
Guess the number C#
Apr 10 2014 7:28 AM
Hi. I am pretty new to C# and trying to get this game to work. I got a textbox where I type the number that I have in mind and the computer must guess it using bisection algorithm but I don't understand why the loop won't work since the condition is true. Here's my code. The biggeest and the minimum have their values set to "0" and "100".
private void btnGuess_Click(object sender, EventArgs e)
{
bool guess = true;
Convert.ToInt32(txtNumber.Text);
nrToGuess = int.Parse(txtNumber.Text);
DialogResult dialogResult;
do
{
average = minimum + biggest / 2;
dialogResult= MessageBox.Show("Is your number" + " " + average+ " ? ", "Question", MessageBoxButtons.YesNo);
if (dialogResult== DialogResult.No && average > nrToGuess)
{
biggest = average;
average = biggest + minimum / 2;
}
else if (dialogResult == DialogResult.No && average < nrToGuess)
{
minimum = average;
average = biggest + minimum / 2;
}
else if (dialogResult == DialogResult.Yes)
{
dialogResult=MessageBox.Show("Congratulations! You guessed the number!");
break;
}
else
{
Application.Exit();
}
}
while (guess);
I would really appreciate any help given. Thank you very much!
Reply
Answers (
2
)
Mapping Microsoft Excel columns programmatically
TextBox.Focus