如何在每个标签中记录每个数字以进行计算?

huangapple go评论86阅读模式
英文:

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 !");
                }

huangapple
  • 本文由 发表于 2020年1月3日 23:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定