Need help with a C# windows program

Started by
35 comments, last by duHOSSval 16 years, 1 month ago
I can't tell if it is working because the program freezes after a few guesses. WHat about this?

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Program4{    public partial class Form1 : Form    {                Random r = new Random();        int answer;                                                    public Form1()        {            InitializeComponent();        }        public int GetRandomNumber()        {            answer = r.Next(0,10);            return answer;        }        private void guessbutton_Click(object sender, EventArgs e)        {            answer = GetRandomNumber();            int count = 0;            //answer = r.Next(0, 10);            int guess;            guess = Convert.ToInt32(textBox1.Text);            //int count = 0;            //int seeded = 0;            //string inValue;            //int guess;            //inValue = textBox1.Text;            //guess = int.Parse(inValue);            ////Random r = new Random();            //while (seeded == 0)            //{            //    Random r = new Random();            //    int answer = r.Next(0, 10);            //    seeded++;                            //    if (guess < answer)            //    {            //        this.BackColor = System.Drawing.Color.Pink;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess > answer)            //    {            //        this.BackColor = System.Drawing.Color.Blue;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess == answer)            //    {            //        this.textBox1.Clear();            //        MessageBox.Show("The number of guesses are " + count);            //        break;            //    }            //    count++;            //}             do            {                count += 1;                //int guess;                //guess = Convert.ToInt32(textBox1);                if (guess != answer)                    if (guess < answer)                    {                        this.BackColor = System.Drawing.Color.Pink;                        this.textBox1.Clear();                        break;                    }                    else                        if(guess > answer)                    {                        this.BackColor = System.Drawing.Color.Blue;                    }                else                {                                        MessageBox.Show("The number of guesses are" + count);                }            }            while (guess < 101);                        }        private void textBox1_TextChanged(object sender, EventArgs e)        {                                           }        private void yourguess_Click(object sender, EventArgs e)        {        }    }}]
Advertisement
I can't tell if it is working because the program freezes after a few guesses. WHat about this?

[/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Program4{    public partial class Form1 : Form    {                Random r = new Random();        int answer;                                                    public Form1()        {            InitializeComponent();        }        public int GetRandomNumber()        {            answer = r.Next(0,10);            return answer;        }        private void guessbutton_Click(object sender, EventArgs e)        {            answer = GetRandomNumber();            int count = 0;            //answer = r.Next(0, 10);            int guess;            guess = Convert.ToInt32(textBox1.Text);            //int count = 0;            //int seeded = 0;            //string inValue;            //int guess;            //inValue = textBox1.Text;            //guess = int.Parse(inValue);            ////Random r = new Random();            //while (seeded == 0)            //{            //    Random r = new Random();            //    int answer = r.Next(0, 10);            //    seeded++;                            //    if (guess < answer)            //    {            //        this.BackColor = System.Drawing.Color.Pink;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess > answer)            //    {            //        this.BackColor = System.Drawing.Color.Blue;            //        this.textBox1.Clear();            //        count++;            //        break;            //    }            //    else if (guess == answer)            //    {            //        this.textBox1.Clear();            //        MessageBox.Show("The number of guesses are " + count);            //        break;            //    }            //    count++;            //}             do            {                count += 1;                //int guess;                //guess = Convert.ToInt32(textBox1);                if (guess != answer)                    if (guess < answer)                    {                        this.BackColor = System.Drawing.Color.Pink;                        this.textBox1.Clear();                        break;                    }                    else                        if(guess > answer)                    {                        this.BackColor = System.Drawing.Color.Blue;                    }                else                {                                        MessageBox.Show("The number of guesses are" + count);                }            }            while (guess < 101);                        }        private void textBox1_TextChanged(object sender, EventArgs e)        {                                           }        private void yourguess_Click(object sender, EventArgs e)        {        }    }}]
Quote:Original post by duHOSSval
Let me see if I am understanding this properly. I need to put the
answer = r.Next(0,10) outside of the buttonclick method? Like right under where i have the Random r = new random() ?


You can't put it right under there. The only things you can put there are member variable declarations and things like that... not executable code (except for initializers... for example, you were able to set it equal to "new Random()"). You need to put it in a place where executable code is legal--in other words, it needs to be in a class method.
Figure out why you keep posting like 10 times.

I am not sure why it is freezing, it might be out of the scope of the code we are looking at.

Look at this code for a second.

        private void guessbutton_Click(object sender, EventArgs e)        {            answer = GetRandomNumber();
In plain english that says, "After a guess has been made decide what the answer is."

Your are doing this.
Phil asks Steve to guess the number he is thinking of.{	Steve guesses 7.	Phil now at this point decides the number is 6.	Steve is wrong.}{	Steve guesses 6.	Phil now at this point decides the number is 5.	Steve is wrong.}



You need to be doing this.
Phil thinks of the number 6.Phil asks Steve to guess the number he is thinking of.{	Steve guesses 7.	Steve is wrong.}{	Steve guesses 6.	Steve is right.}
Quote:Original post by duHOSSval
I can't tell if it is working because the program freezes after a few guesses. WHat about this?

*** Source Snippet Removed ***

That's exactly what you were doing before. I'm sorry, I've really tried to help, but you keep doing the same thing over and over.

If you don't understand that this...
void MyFunction(){  DoSomething();}

...is the same as this...
void MyFunction(){  MyOtherFunction();}void MyOtherFunction(){  DoSomething();}

...then I don't think you are ready for this class. You aren't understanding some fundamentals of programming logic--let alone the language you are using--that you must understand to succeed in this. I think you need to take a step back (maybe go back to the beginning) and look at the fundamentals.
Might I also suggest trying to wrap your head around debugging, which will allow you to actually see whats happening withing the program.
http://msdn2.microsoft.com/en-us/library/kya29xtx(VS.71).aspx

Though I have to agree that if you cannot understand the basic logic involved in this assignment, you might want find some basic primers and start from there.




-------------------------------------------------------- What''s the difference between engineers and architects? Engineers build weapons; architects build targets. --------------------------------------------------------
What if i put the answer = r.Next(0,10) in the public form1()

This topic is closed to new replies.

Advertisement