我的问题是我的程序在button21的功能之间循环有问题,这取决于if语句写入的顺序if if功能是否有效但另一方不会。 在下面的代码中,我将它设置为button2,但是如果选择的话,我希望按钮1和2都可以与button21一起使用。但是函数设置qw == 1是在这个程序中工作的不是qw == 2 那我的程序代码有什么问题? 代码显示:
namespace Matematisk_indlæring
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random RND = new Random(Guid.NewGuid().GetHashCode());
Random RND2 = new Random(Guid.NewGuid().GetHashCode());
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Hide();
button2.Hide();
button3.Hide();
button4.Hide();
button5.Hide();
label1.Show();
textBox1.Show();
button21.Show();
double qw = 1;
textBox2.Text = qw.ToString();
string q = "1+1";
label1.Text = q;
int qq = 1 + 1;
textBox3.Text = qq.ToString();
}
private void button21_Click(object sender, EventArgs e)
{
double qqq = Convert.ToDouble(textBox1.Text);
double qq = Convert.ToDouble(textBox3.Text);
int qw = Convert.ToInt32(textBox2.Text);
if (qw == 1)
{
if (qq == qqq)
{
MessageBox.Show("succes");
int qws1;
int qws;
qws1 = RND2.Next(51, 100);
qws = RND.Next(0, 50);
qq = qws1 + qws;
textBox3.Text = qq.ToString();
string tese = qws.ToString();
string tese2 = qws1.ToString();
label1.Text = tese2 + "+" + tese;
}
if (qw == 2)
{
if (qq == qqq)
{
MessageBox.Show("succes");
int qws1;
int qws;
qws1 = RND2.Next(51, 100);
qws = RND.Next(0, 50);
qq = qws1 - qws;
textBox3.Text = qq.ToString();
string tese = qws.ToString();
string tese2 = qws1.ToString();
label1.Text = tese2 + "-" + tese;
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
button1.Hide();
button2.Hide();
button3.Hide();
button4.Hide();
button5.Hide();
label1.Show();
textBox1.Show();
button21.Show();
double qw = 2;
textBox2.Text = qw.ToString();
string q = "1-1";
label1.Text = q;
int qq = 1 - 1;
textBox3.Text = qq.ToString();
}
}
}
答案 0 :(得分:1)
这里至少有一个逻辑漏洞。
在button21_Click
中:首先测试qw
是否为1,然后测试它是否为2.但是没有任何代码更改qw
。那怎么突然从1变为2?
此方法中的代码基本上是:
int qw = Convert.ToInt32(textBox2.Text);
if (qw == 1)
{
if (qq == qqq)
{
// code which does not modify qw
}
if (qw == 2) // wrong placement of this if-statement!
{
// code which can never be called!
}
}
正如您所看到的,控件在此方法中永远不会达到if (qw == 2)
因为您嵌套了ifs错误。如果您修复了缩进,您可以更轻松地看到它。
另外:Du burde bruke engelsk klassenavn,ikke dansk :-)