英文:
How to record each number for calculation in each label?
问题
Currently, I use the if-else statement to indicate that if label.text is 25, it will stop adding. It does work, But it work one label only. Other label can't continue to add the number if I type the related subject.
if (label21.Text != "25" && label20.Text != "25" && label19.Text != "25" && label18.Text != "25" && label17.Text != "25")
{
if (listBox1.Items.Count != 5)
{
string sbj_inc = textBox7.Text;
switch (sbj_inc)
{
case "Physics":
phy = phy + 1;
label21.Text = phy.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Chemistry":
che = che + 1;
label20.Text = che.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "English":
eng = eng + 1;
label9.Text = eng.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Mandarin":
bc = bc + 1;
label18.Text = bc.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Melayu":
bm = bm + 1;
label17.Text = bm.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
default:
MessageBox.Show("Invalid Subject");
break;
}
}
else
{
MessageBox.Show("Maximum 4 Subjects can be chosen !");
}
}
else
{
MessageBox.Show("Class Full");
}
英文:
Currently I use the if-else statement to indicate that if label.text is 25, it will stop adding.
It does work, But it work one label only.Other label can't continue add the number if I type the related subject.
if (label21.Text != "25" && label20.Text != "25" && label19.Text != "25" && label18.Text != "25" && label17.Text != "25")
{
if (listBox1.Items.Count != 5)
{
string sbj_inc = textBox7.Text;
switch (sbj_inc)
{
case "Physics":
phy = phy + 1;
label21.Text = phy.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Chemistry":
che = che + 1;
label20.Text = che.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "English":
eng = eng + 1;
label9.Text = eng.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Mandarin":
bc = bc + 1;
label18.Text = bc.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
case "Melayu":
bm = bm + 1;
label17.Text = bm.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
break;
default:
MessageBox.Show("Invalid Subject");
break;
}
}
else
{
MessageBox.Show("Maximum 4 Subjects can be chosen !");
}
}
else
{
MessageBox.Show("Class Full");
}
答案1
得分: 2
这是我评论和@ADyson的快速重写:
if (listBox1.Items.Count != 5)
{
string sbj_inc = textBox7.Text;
switch (sbj_inc)
{
case "物理":
if (label21.Text != "25"){
phy = phy + 1;
label21.Text = phy.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "化学":
if (label20.Text != "25"){
che = che + 1;
label20.Text = che.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "英语":
if (label19.Text != "25"){
eng = eng + 1;
label9.Text = eng.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "普通话":
if (label18.Text != "25"){
bc = bc + 1;
label18.Text = bc.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "马来语":
if (label17.Text != "25"){
bm = bm + 1;
label17.Text = bm.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
default:
MessageBox.Show("无效科目");
break;
}
}
else
{
MessageBox.Show("最多可选择 4 门科目!");
}
注意:我将代码中的科目名称从HTML实体编码转换为普通文本。如果需要其他帮助,请随时提问。
英文:
Here's the quick rewrite from my comment and @ADyson:
if (listBox1.Items.Count != 5)
{
string sbj_inc = textBox7.Text;
switch (sbj_inc)
{
case "Physics":
if (label21.Text != "25"){
phy = phy + 1;
label21.Text = phy.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "Chemistry":
if (label20.Text != "25"){
che = che + 1;
label20.Text = che.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "English":
if (label19.Text != "25"){
eng = eng + 1;
label9.Text = eng.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "Mandarin":
if (label18.Text != "25"){
bc = bc + 1;
label18.Text = bc.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
case "Melayu":
if (label17.Text != "25"){
bm = bm + 1;
label17.Text = bm.ToString();
listBox1.Items.Add(textBox7.Text + "\t" + textBox8.Text);
}
break;
default:
MessageBox.Show("Invalid Subject");
break;
}
}
else
{
MessageBox.Show("Maximum 4 Subjects can be chosen !");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论