案例切换中的表达式无效

时间:2014-10-15 22:53:45

标签: c# winforms switch-statement

这一直让我疯狂。

private void BtnMix_Click(object sender, EventArgs e)
{
   //Declare the variables to be used in the process
    String Color1;
    String Color2;

    //Determine whether or not a selection has been bade
    if (Colorlist1.SelectedIndex != -1 && ColorList2.SelectedIndex != -1) 
    {
        //Get the selected colors
        Color1 = Colorlist1.SelectedItem.ToString();
        Color2 = ColorList2.SelectedItem.ToString();

        //Determine the correct result
        switch (Color1 +|+ Color2)
        {
            case "Red|Red":
                lblResult.Text = "Red";
                break;
            case "Red|Blue":
                Lblresult.text ="Purple";
                break;
            case "Red|Yellow":
                lblresult.text = "Orange";
                break;
            case "Blue|Red":
                lblresult.text = "Purple";
                break;
            case "Blue|Blue":
                lblresult.text = "Blue";
                break;
            case "Blue|Yellow":
                lblresult.text = "Green";
                break;
            case "Yellow|Red":
                lblresult.text = "Orange";
                break;
            case "Yellow|Blue":
                lblresult.text = "Green";
                break;
            case "Yellow|Yellow":
                lblresult.text = "Yellow";
                break;
        }
    }
    else
    {
        MessageBox.Show("Please select two colors");
    }
}

我知道有一些非常容易让我失踪,但对于我的生活,我无法理解。到目前为止,我已经设法使一切“正常”,但这最后的错误似乎没有任何意义。 “术语'|'的表达式无效“第15行。

2 个答案:

答案 0 :(得分:4)

如果您愿意连接字符串,请使用

Color1 + "|" + Color2

所以,现在你的代码应该是这样的:

switch (Color1 + "|" + Color2)

另外,你会在第21行遇到另一个错误,因为你在Lblresult

中使用了大写的“L”而不是小写的“l”

答案 1 :(得分:-1)

显示无效的表达式术语开关     课程     {         static void Main()         {             int Totalcoffeecost = 0;             string Userdecision = string.Empty;             做             {                 int userchoice = -1;                 做             {

        Console.WriteLine("Please enter your coffee size: 1-small, 2-Medum,3-Large");
       userchoice = int.Parse(Console.ReadLine());
            }while(





     switch (userchoice)
        {
            case 1:
                Totalcoffeecost += 1;
                break;
            case 2:
                Totalcoffeecost += 2;
                break;
            case 3:
                Totalcoffeecost += 3;
                break;
            default:
                Console.WriteLine("Your choice {0} is invalid", userchoice);
                break;
        } 
        while(userchoice !=1 && userchoice !=2 && userchoice !=3)
            do
            {
                Console.WriteLine("Do you want to buy another coffee-Yes or No");
                string userdecision = "";
                userdecision = Console.ReadLine().ToUpper();
                if(userdecision != "Yes" && userdecision !="No")

                {
                    Console.WriteLine("your choice {0} is invalid. Please try again",userdecision);
                }
            }while(Userdecision != "Yes" && Userdecision != "No");
            }
        while(Userdecision.ToUpper()!="No");
        Console.WriteLine("Thank you for shopping with us");
        Console.WriteLine("Bill amount={0}",Totalcoffeecost );


        }
        }