-
Problem with a math tutor
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private TextBox textBox1;
private TextBox textBox3;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private RadioButton radioButton5;
private Button button1;
private TextBox nameBox;
private Button nameButton;
private Label label1;
private Button buttonCalculate;
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.buttonCalculate = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.button1 = new System.Windows.Forms.Button();
this.nameBox = new System.Windows.Forms.TextBox();
this.nameButton = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(212, 112);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 5;
this.textBox1.TabStop = false;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(212, 207);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 20);
this.textBox3.TabIndex = 7;
//
// buttonCalculate
//
this.buttonCalculate.Location = new System.Drawing.Point(258, 276);
this.buttonCalculate.Name = "buttonCalculate";
this.buttonCalculate.Size = new System.Drawing.Size(75, 23);
this.buttonCalculate.TabIndex = 8;
this.buttonCalculate.Text = "Calculate";
this.buttonCalculate.UseVisualStyleBackColor = true;
this.buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(27, 112);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(44, 17);
this.radioButton1.TabIndex = 9;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "Add";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.Click += new System.EventHandler(this.radioButton1_CheckedChanged);
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(27, 145);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(65, 17);
this.radioButton2.TabIndex = 10;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "Subtract";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.Click += new System.EventHandler(this.radioButton2_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(27, 184);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(60, 17);
this.radioButton3.TabIndex = 11;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "Multiply";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.Click += new System.EventHandler(this.radioButton3_CheckedChanged);
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(27, 219);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(55, 17);
this.radioButton4.TabIndex = 12;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "Divide";
this.radioButton4.UseVisualStyleBackColor = true;
this.radioButton4.Click += new System.EventHandler(this.radioButton4_CheckedChanged);
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);
//
// radioButton5
//
this.radioButton5.AutoSize = true;
this.radioButton5.Location = new System.Drawing.Point(27, 253);
this.radioButton5.Name = "radioButton5";
this.radioButton5.Size = new System.Drawing.Size(41, 17);
this.radioButton5.TabIndex = 13;
this.radioButton5.TabStop = true;
this.radioButton5.Text = "Mix";
this.radioButton5.UseVisualStyleBackColor = true;
this.radioButton5.Click += new System.EventHandler(this.radioButton5_CheckedChanged);
this.radioButton5.CheckedChanged += new System.EventHandler(this.radioButton5_CheckedChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(140, 276);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// nameBox
//
this.nameBox.Location = new System.Drawing.Point(53, 32);
this.nameBox.Name = "nameBox";
this.nameBox.Size = new System.Drawing.Size(100, 20);
this.nameBox.TabIndex = 15;
//
// nameButton
//
this.nameButton.Location = new System.Drawing.Point(225, 32);
this.nameButton.Name = "nameButton";
this.nameButton.Size = new System.Drawing.Size(75, 23);
this.nameButton.TabIndex = 16;
this.nameButton.Text = "Enter name";
this.nameButton.UseVisualStyleBackColor = true;
this.nameButton.Click += new System.EventHandler(this.nameButton_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 37);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 17;
this.label1.Text = "Name";
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(356, 364);
this.Controls.Add(this.label1);
this.Controls.Add(this.nameButton);
this.Controls.Add(this.nameBox);
this.Controls.Add(this.button1);
this.Controls.Add(this.radioButton5);
this.Controls.Add(this.radioButton4);
this.Controls.Add(this.radioButton3);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.buttonCalculate);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox1);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();
}
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
public frmMain()
{
InitializeComponent();
}
int op1;
int op2;
int ans;
int userAns;
string name;
public bool add = false;
public bool sub = false;
public bool multiply = false;
public bool divide = false;
public bool mix = false;
private void nameButton_Click(object sender, EventArgs e)
{
name = nameBox.Text;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
add = true;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
sub = true;
add = false;
-
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
multiply = true;
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
divide = true;
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
mix = true;
}
private void button1_Click(object sender, EventArgs e)
{
Random a = new Random();
op1 = a.Next(1, 100);
op2 = a.Next(1, 100);
if (add = true)
{
ans = op1 + op2;
textBox1.Text = op1 + "+" + op2;
}
else if (sub = true)
{
ans = op1 - op2;
textBox1.Text = op1 + "-" + op2;
}
else if (multiply = true)
{
ans = op1 * op2;
textBox1.Text = op1 + "*" + op2;
}
else if (divide = true)
{
ans = op1 / op2;
textBox1.Text = op1 + "/" + op2;
}
else if (mix = true)
{
ans = op1 * op2;
textBox1.Text = op1 + "*" + op2;
}
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
userAns = int.Parse(textBox3.Text);
if (userAns == ans)
MessageBox.Show("Good job!");
}
}
For some reason, when I run it, I keep getting addition problems.
-
It's usually a good idea to post code but ALL the code is overkill. The designer generated stuff that sets up your controls won't help us solve the problem any better.
What kind of addition problem are you having? Are you not getting the result you expected? One thing I noticed is that when your radiobuttons change state you set the corrisponding boolean value to true. However, what happens when the radiobutton is unchecked?!?! For instance the addition radiobutton sets the boolean to true. But the checkchanged event will occur when a radiobutton is selected/unselected. You should be setting the boolean values to the checked value of that radiobutton instead. This way the value is true if checked and false if unchecked which is what you want Otherwise addition, as well as the other operators, will be stuck on. I suspect this is your problem and changing this should fix it.
Similar Threads
-
By root computing in forum C++
Replies: 5
Last Post: 01-05-2007, 11:06 PM
-
By dbrook007 in forum ASP.NET
Replies: 0
Last Post: 11-06-2006, 04:54 AM
-
By koraal in forum VB Classic
Replies: 3
Last Post: 11-03-2006, 03:50 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Wade Balzer in forum VB Classic
Replies: 0
Last Post: 06-23-2000, 02:17 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks