Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#增加超出预期的增量_C# - Fatal编程技术网

C#增加超出预期的增量

C#增加超出预期的增量,c#,C#,我正在编写一个石头剪纸游戏,不知为什么计数器会变得越来越奇怪。谁能解释一下为什么当我赢的时候会增加1,当我输的时候会增加2,当我打成平局的时候会增加3?我只想加1。我感谢所有的答案 class RPSLS { public enum GameMode { Unknown, Single_Player, Two_Player, } public GameMode GameType

我正在编写一个石头剪纸游戏,不知为什么计数器会变得越来越奇怪。谁能解释一下为什么当我赢的时候会增加1,当我输的时候会增加2,当我打成平局的时候会增加3?我只想加1。我感谢所有的答案

class RPSLS
{        
    public enum GameMode
    {
        Unknown,
        Single_Player,
        Two_Player,            
    }
    public GameMode GameType {get; set;}
    public enum Hand { Rock = 1, Paper, Scissors, Lizard, Spock, Report, Exit };
    public enum Outcome { Win, Lose, Tie };

    public Hand ComputerHand { get; set; }
    public Hand PlayerHand { get; set; }
    public char UserSelection { get; set; }
   int win = 0;
    int lose = 0;
    int tie = 0;
    public Hand getUserHand()
    {
        while (!validateSelection())
        {
            Console.Clear();
            Console.WriteLine("Invalid Input");
            UserSelection = Convert.ToChar(Console.ReadLine());
        }

        switch (Char.ToUpper(UserSelection))
        {
            case 'R':
                PlayerHand = Hand.Rock;
                break;
            case 'P':
                PlayerHand = Hand.Paper;
                break;
            case 'S':
                PlayerHand = Hand.Scissors;
                break;
            case 'L':
                PlayerHand = Hand.Lizard;
                break;
            case 'K':
                PlayerHand = Hand.Spock;
                break;
            case 'Q':
                PlayerHand = Hand.Exit;
                break;
            case 'T':
                PlayerHand = Hand.Report;
                break;
            default:
                throw new Exception("Unexpected Error");
        }
        return PlayerHand;
    }

    public Outcome DetermineWinner()
    {
        if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Paper)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Lizard)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Rock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Spock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Paper)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Spock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Rock)
        {
            win++;
            return Outcome.Win;
        }

        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Rock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Spock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Paper)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Spock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Scissors)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Lizard)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Rock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Scissors)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Lizard)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Paper)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Exit)
        {
            Environment.Exit(0);
        }
        else if (PlayerHand == Hand.Report)
        {
            Report();
        }
        tie++;
        return Outcome.Tie;
    }

    private void Report()
    {
        Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
    }

    private void PlayGame()
    {
        bool gameOver = false;
        var rand = new Random();
        char response;

        while (!gameOver)
        {
            Screen();
            UserSelection = Convert.ToChar(Console.ReadLine());
            getUserHand();
            ComputerHand = (Hand)rand.Next(1, 6);
            Console.Clear();
            Console.WriteLine("Computer's Hand: {0}", ComputerHand);
            Console.WriteLine("Player's Hand: {0}", PlayerHand);

            if (DetermineWinner() == Outcome.Win)
                Console.WriteLine("Win");
            else if (DetermineWinner() == Outcome.Lose)
                Console.WriteLine("Lose");
            else if (DetermineWinner() == Outcome.Tie)
                Console.WriteLine("Tie");

            Console.WriteLine("Would you Like to play another game? (y/n)");
            response = Convert.ToChar(Console.ReadLine());

            while(validateResponse(response) == false)
            {
                Console.WriteLine("Invalid Input, Please re-enter your selection: ");
                response = Convert.ToChar(Console.ReadLine());
            }

            if (response == 'N' || response == 'n')
                gameOver = true;

            Console.Clear();
        }
    }

    public bool validateResponse (char response)
    {
        if (Char.ToUpper(response) != 'Y' && Char.ToUpper(response) != 'N')
            return false;

        return true;
    }
    private bool validateSelection()
    {
        char value = Char.ToUpper(UserSelection);
        if (value != 'R' && value != 'P' && value != 'S' && value != 'L' && value != 'K' && value != 'T' && value != 'Q')
            return false;
        return true;
    }
    private void Screen()
    {
        Console.WriteLine("(R)ock | (P)aper | (S)cissors | (L)izard | Spoc(k) | (Q)uit | Repor(t)");

    }
   public GameMode getGameType()
    {
        bool flag = true;
        string buffer;
        GameMode GameType = GameMode.Unknown;

        GameType = GameMode.Unknown;
        Console.WriteLine("Available options are: (S)ingle Player | (T)wo Player");
        Console.Write("What would you like to play?: ");

        while (flag)
        {
            buffer = Console.ReadLine();
            try
            {
                switch (buffer.ToUpper())
                {
                    case "S":
                    case "SINGLE PLAYER":
                        GameType = GameMode.Single_Player;
                        break;
                    case "T":
                    case "TWO PLAYER":
                        GameType = GameMode.Two_Player;
                        break;

                    default:
                        Console.WriteLine("Action not understood.");
                        break;
                }
                if (GameType != GameMode.Unknown)
                {
                    PerformWork(GameType);
                }
            }
            catch (ArgumentException)
            {
                Console.WriteLine("'{0}' is not understood.", buffer);
            }

        }
        return GameType;
    }

    private void PerformWork(GameMode cmd)
    {
        if (cmd == GameMode.Single_Player)
        {
            Console.Clear();
            SinglePlayer(cmd);


        }
        else if (cmd == GameMode.Two_Player)
        {
            Console.WriteLine("Two Player Mode");
        }
        else
        {
            Console.WriteLine("Action not understood.");
        }
    }
    private void SinglePlayer(GameMode cmd)
    {
        Console.WriteLine("Hello. You are now playing Single Player Mode");
        Console.Write("Please Enter Your Name: ");
        String nameInput = Console.ReadLine();
        Console.Write("Please Enter A Name For Computer: ");
        string computerNameInput = Console.ReadLine();
        Console.Clear();
      Console.WriteLine(nameInput + " vs " + computerNameInput);

        PlayGame();
    }
}
}

这段代码的副作用将增加类级变量

 if (DetermineWinner() == Outcome.Win)
    Console.WriteLine("Win");
 else if (DetermineWinner() == Outcome.Lose)
    Console.WriteLine("Lose");
 else if (DetermineWinner() == Outcome.Tie)
    Console.WriteLine("Tie");

每次调用
DetermineWinner()
时,字段都会被触动。

当调用DetermineWinner()时,您的值会递增,一次调用3次,一次调用平局,两次调用输局,一次调用赢局。 例如,假设结果是平局,则调用DetermineWinner()检查是否是满足函数中平局条件并递增值的赢。计算失败,因此它运行else并再次调用DetermineWinner(),whcih再次递增


修复方法是调用DetermineWinner()一次,并将结果赋给一个变量,然后您就可以在不重新增量的情况下对其进行求值。

Holy code dump,蝙蝠侠!请减少您的问题,使其仅包含一个可靠地再现您的问题的好问题。谢谢您的回复!我不确定问题到底出在哪里,这就是我发布整个代码的原因=/该页面提供了一些关于如何以及为什么提供好的MCVE的好建议。在该页面的底部有一些关于该主题的扩展链接。您还可以再次查看页面和底部的链接。不确定问题在哪里,并不意味着询问问题的人可以不做尽职调查,将问题缩小到一个适当的范围(包括完整的,上面也没有)。顺便说一句,你的代码可以大大简化。要获得灵感,请查看以下javascript版本:。受我对这个问题的回答启发: