输出Messagebox消息的方法

时间:2013-12-08 20:38:25

标签: c#

是否有更好的方法来呈现以下方法,根据参数打印出不同的消息。发现它太冗长冗长而且不能简单地说每条线路的消息不同。如果我可以缩短它,请提出建议。谢谢。

private void message(int choice, string result)
{
    if (choice == 1 && result == "Draw")
    {
        MessageBox.Show("It is a draw. Both chose Rock");
    }
    else if (choice == 2 && result == "Draw")
    {
        MessageBox.Show("It is a draw. Both chose Paper");
    }
    else if (choice == 3 && result == "Draw")
    {
        MessageBox.Show("It is a draw. Both chose Scissor"); 
    }

    else if (choice == 1 && result == "Win")
    {
        MessageBox.Show("Congratulations! Rock beats Scissor");
    }
    else if (choice == 2 && result == "Win")
    {
        MessageBox.Show("Congratulations! Paper beats Rock");
    }
    else if (choice == 3 && result == "Win")
    {
        MessageBox.Show("Congratulations! Scissor beats Paper");
    }

    else if (choice == 1 && result == "Lose")
    {
        MessageBox.Show("You lose. Paper beats rock");
    }
    else if (choice == 2 && result == "Lose")
    {
        MessageBox.Show("You lose. Scissor beats Paper");
    }
    else if (choice == 3 && result == "Lose")
    {
        MessageBox.Show("You lose. Rock beats Scissor");
    }
}

5 个答案:

答案 0 :(得分:4)

设置枚举以表示可能的选择:

public enum Choice
{
    Rock = 1,
    Paper = 2,
    Scissor = 3
}

然后使用string.Format()

var selectedChoice = Enum.GetName(typeof(Choice), choice);

var beatsChoice = selectedChoice == Choice.Rock ? Choice.Scissor
                    : (selectedChoice == Choice.Paper ? Choice.Rock : Choice.Paper);

if (result == "Draw")
    MessageBox.Show(string.Concat("It is a draw. Both chose ", selectedChoice);
else if (result == "Win")
    MessageBox.Show(string.Concat("Congratulations! ", selectedChoice, " beats ", beatsChoice);
else if (result == "Lose")
    MessageBox.Show(string.Concat("You lose. ", selectedChoice, " beats ", beatsChoice);

答案 1 :(得分:1)

不知道这是不是一个好主意,但你可以这样做:

private void message(int choice, string result)
        {
           if(result == "Draw")
           {
            switch (choice)
            case 1: MessageBox.Show("It is a draw. Both chose Rock");
            break;
            case 2: MessageBox.Show("It is a draw. Both chose Paper");
            break;
            case 3: MessageBox.Show("It is a draw. Both chose Scissor");
            break;
           }
           //similarly do for the rest.
        }

答案 2 :(得分:1)

我不确定我喜欢你的设计,但鉴于你的设计,它(稍微)更好维护......

private void message(int choice, string result)
{
   string self;
   switch(choice)
   {
     case 1: self = "Rock"; break;
     case 2: self = "Paper"; break;
     case 3: self = "Scissor"; break;
   }

   switch(result)
   {
     case "Draw":
       MessageBox.Show(String.Format("It is a draw. Both chose {0}", self));
       break;

     case "Win":
       string beats;
       switch(choice)
       {
         case 1: beats = "Scissor"; break;
         case 2: beats = "Rock"; break;
         case 3: beats = "Paper"; break;
       }
       MessageBox.Show(String.Format("Congratulations! {0} beats {1}", self, beats));
       break;

     case "Lose":
       string loses;
       switch(choice)
       {
         case 1: loses = "Paper"; break;
         case 2: loses = "Scissor"; break;
         case 3: loses = "Rock"; break;
       }
       MessageBox.Show(String.Format("You lose. {1} beats {0}", self, loses));
       break;
       break;
   }
}

那就是说,这个解决方案仍然非常丑陋。更好的方法是绕过封装岩石/纸张/剪刀状态而不是“选择”整数的物体。这些将有方法来获得他们击败的东西。然后打印就会简单得多。

答案 3 :(得分:0)

另一件事是你可以将整数和字符串设为Enum,这样就可以打印出极少数代码行的措辞。

答案 4 :(得分:0)

使用枚举来提高可读性。我也会使用两个参数,因为不清楚谁赢了:

public enum RockPaperScissors
{ 
    Rock,
    Paper,
    Scissor
}

public enum GameResult
{ 
    PlayerOneWin,
    PlayerTwoWin,
    Draw
}

然后该方法可以缩短为:

private void message(GameResult result, RockPaperScissors choice1, RockPaperScissors choice2)
{
    if (result == GameResult.Draw)
    {
        MessageBox.Show("It is a draw. Both chose " + choice1.ToString());                
    }
    else
    {
        string message = string.Format("Congratulations, {0} beats {1}!"
            , result == GameResult.PlayerOneWin ? choice1 : choice2
            , result == GameResult.PlayerOneWin ? choice2 : choice1);
        MessageBox.Show(message);
    }
}