switch语句未按预期应用按钮标记

时间:2015-07-29 20:54:31

标签: c# wpf

在我的程序中,switch语句用于指定特定按钮的特定属性,其中一个属性是指定一个'正确的'用户需要单击以正确回答问题的按钮的标记。我遇到的问题是,由于某种原因,不正确的按钮显示为“#34;你猜错了”#34;仅当带有'正确'的按钮时的消息标签应该这样做。

        public void SetButtonContent(string chosenMetal, TextBlock reagentText, TextBlock transMetalText,
        Button opt1, Button opt2, Button opt3, Button opt4, Button opt5, Button opt6, Button opt7, Button opt8)
    {
        string pickedMetal = chosenMetal;
        string pickedReagent = CycleThroughReagents();

        reagentText.Text = pickedReagent;
        transMetalText.Text = pickedMetal;

        BrushConverter bc = new BrushConverter();
        switch (pickedMetal)
        {
            case "Copper": //this is only one out of six cases, but all the cases have relatively the same format.
                opt1.Content = string.Format("{0} \n {1} \n{2}", Cu.hexAqColour, Cu.hexAqFormula, Cu.hexAqState);
                opt1.Background = Brushes.Blue;

                opt2.Content = string.Format("{0} \n {1} \n{2}", Cu.dilNaOHRctColour, Cu.dilNaOHRctFormula, Cu.dilNaOHRctState);
                opt2.Background = Brushes.Blue;

                opt3.Content = string.Format("{0} \n {1}", Cu.excessNaOHColour, Cu.excessNaOHFormula, Cu.excessNahOHState);
                opt3.Background = Brushes.Blue;

                opt4.Content = string.Format("{0} \n {1} \n{2}", Cu.dilNH3RctColour, Cu.dilNH3RctFormula, Cu.dilNH3RctState);
                opt4.Background = Brushes.Blue;

                opt5.Content = string.Format("{0} \n {1} \n{2}", Cu.ExcessNH3Colour, Cu.ExcessNH3Formula, Cu.ExcessNH3State);
                opt5.Background = Brushes.DarkBlue;

                opt6.Content = string.Format("{0} \n {1} \n{2}", Cu.saltRctColour, Cu.saltRctFormula, Cu.saltRctState);

                opt7.Content = string.Format("{0} \n {1} \n{2}", Cu.Na2CO3RctColour, Cu.Na2CO3RctFormula, Cu.Na2CO3RctState);
                opt7.Background = Brushes.Blue;

                opt8.Content = string.Format("{0} \n {1} \n{2}", Cu.ClRctColour, Cu.ClRctFormula, Cu.ClRctState);
                opt8.Background = Brushes.Yellow;

                if (pickedReagent == reagentsList[0])
                    opt1.Tag = "correct";

                if (pickedReagent == reagentsList[1] || pickedReagent == reagentsList[3])
                    opt2.Tag = "correct";
                    opt4.Tag = "correct";

                if (pickedReagent == reagentsList[2] || pickedReagent == reagentsList[5])
                    opt3.Tag = "correct";
                    opt6.Tag = "correct";

                if (pickedReagent == reagentsList[4])
                    opt5.Tag = "correct";

                if (pickedReagent == reagentsList[6])
                    opt7.Tag = "correct";

                else
                    opt8.Tag = "correct";
                break;



            default:
                break;
        }
    }

以上方法用于设置按钮内容,然后下面的方法在我的主要内容中。处理每个被点击按钮的代码。

    public void CheckForCorrect(Button button)
    {
        if ((string)button.Tag == "correct" && amtLeft != 0)
        {
            amtLeft -= 1;
            MessageBox.Show("You guessed correct!");
            _GameControl.SetButtonContent(chosenMetal, ReagentAdded, transMetal, Opt1, Opt2, Opt3, Opt4, Opt5, Opt6, Opt7, Opt8);
            button.IsEnabled = false;
        }

        if ((string)button.Tag != "correct" && amtLeft != 0)
        {
            MessageBox.Show("Oops!");
        }

        if (amtLeft == 0)
        {
            SwitchMetals();
        }


    }

按钮内容首先在' MainWindow()'中设置。 class是' SetButtonContent'的第一个实例。在程序中执行。

public MainWindow()
    {
        InitializeComponent();
        chosenMetal = "Copper";
        _GameControl.SetButtonContent(chosenMetal,ReagentAdded, transMetal, Opt1, Opt2, Opt3, Opt4, Opt5, Opt6, Opt7, Opt8);

        Opt1.Click += HandleButtonClicks;
        Opt2.Click += HandleButtonClicks;
        Opt3.Click += HandleButtonClicks;
        Opt4.Click += HandleButtonClicks;
        Opt5.Click += HandleButtonClicks;
        Opt6.Click += HandleButtonClicks;
        Opt7.Click += HandleButtonClicks;
        Opt8.Click += HandleButtonClicks;  
    }

我将不胜感激任何帮助和建议,因为我不确定这是否会发生错误。

2 个答案:

答案 0 :(得分:1)

如果if语句中有多行代码,则需要使用大括号。

在上面的代码中,opt4和opt 6将始终设置为“正确”,因为它们在if语句之外。

此外,如果所有if情况都是独占的,您应该完全指定它们,否则使用else if。代码现在的方式,如果pickedReagentreagentsList[6]以外的任何内容,则opt8始终设置为“正确”。

包含这些修补程序的代码:

if (pickedReagent == reagentsList[0])
{
    opt1.Tag = "correct";
}
else if (pickedReagent == reagentsList[1] || pickedReagent == reagentsList[3])
{
    opt2.Tag = "correct";
    opt4.Tag = "correct";
}
else if (pickedReagent == reagentsList[2] || pickedReagent == reagentsList[5])
{
    opt3.Tag = "correct";
    opt6.Tag = "correct";
}
else if (pickedReagent == reagentsList[4])
{
    opt5.Tag = "correct";
}
else if (pickedReagent == reagentsList[6])
{
    opt7.Tag = "correct";
}
else 
{
    opt8.Tag = "correct";
}

答案 1 :(得分:0)

if (pickedReagent == reagentsList[1] || pickedReagent == reagentsList[3])
                    opt2.Tag = "correct";
                    opt4.Tag = "correct";

如果语句为真,此语句是否设置了opt2.Tag和opt4.Tag?它的书面opt4.Tag总是正确的方式和opt6.Tag也将如此