不确定如何处理烫手山芋游戏中的异常C#

时间:2017-07-20 01:33:40

标签: c# if-statement exception int

我正在用C#做一个热土豆游戏。在代码中我试图处理Game()方法中的异常,它会提示你将马铃薯传递给某人(通过他们的玩家号码)它会阻止你输入你自己的玩家号码(试图将它传递给你自己。) 唯一的问题是,我在尝试处理异常时遇到了麻烦:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotPotatoe
{
    class Program
    {
        public static void Main()
        {
            Console.Clear();
            Console.Write("How many players are there? (up to 4): ");
            string PlayerCount = Console.ReadLine();
            string PlayerOne = null;
            string PlayerTwo = null;
            string PlayerThree = null;
            string PlayerFour = null;
            if (PlayerCount == "2")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 2);
                Game(PlayerOne, PlayerTwo, PlayerThree, PlayerFour, WhoStarts);
            }
            else if (PlayerCount == "3")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Console.Write("Enter player 3's name: ");
                PlayerThree = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 3);
                Game(PlayerOne, PlayerTwo, PlayerThree, PlayerFour, WhoStarts);
            }
            else if (PlayerCount == "4")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Console.Write("Enter player 3's name: ");
                PlayerThree = Console.ReadLine();
                Console.Write("Enter player 4's name: ");
                PlayerFour = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 4);

                Game(PlayerOne, 
                    PlayerTwo, 
                    PlayerThree, 
                    PlayerFour, 
                    WhoStarts);
            }
            else
            {
                Console.WriteLine("Not valid!");
                Console.ReadKey();
                Main();
            }
        }

        private static void Game
           (string player1, 
            string player2, 
            string player3, 
            string player4, 
            int whoStarts)     
        {
            Player Player1 = new Player();  
            Player Player2 = new Player();
            Player Player3 = new Player();
            Player Player4 = new Player();
            Player1.Name = player1;         
            Player2.Name = player2;
            Player3.Name = player3;
            Player4.Name = player4;
            Player1.Number = 1;
            Player2.Number = 2;
            Player3.Number = 3;
            Player4.Number = 4;
            Player1.IsOut = false;   
            Player2.IsOut = false;
            if(Player3.Name == null)
            {
                Player3.IsOut = true;
            }
            else
            {
                Player3.IsOut = false;
            }
            if (Player4.Name == null)
            {
                Player4.IsOut = true;
            }
            else
            {
                Player3.IsOut = false;
            }
            switch (whoStarts)
            {
                case 1:
                    Player1.HasPotatoe = true;
                    break;
                case 2:
                    Player2.HasPotatoe = true;
                    break;
                case 3:
                    Player3.HasPotatoe = true;
                    break;
                default:    
                    Player4.HasPotatoe = true;
                    break;
            }
            List<Player> PlayingList = new List<Player>
            {
                Player1,
                Player2
            };
            if (Player3.IsOut == false)
            {
                PlayingList.Add(Player3);
            }
            if (Player4.IsOut == false)
            {
                PlayingList.Add(Player4);
            }
            Random rand = new Random();
            int NumOfRounds = rand.Next(1, 10);
            do
            {
                foreach (Player p in PlayingList)
                {
                    if (p.HasPotatoe == true)
                    {
                        Console.Write("Player {0} has the potatoe! Who do you want to pass it to? (by number): ", p.Number);
                        string input = Console.ReadLine();
                        if (input == "1")
                        {
                            PlayingList[0].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "2")
                        {
                            PlayingList[1].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "3")
                        {
                            if (Player3.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[2].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if (input == "4")
                        {
                            if (Player4.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[3].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if(input == p.Number.ToString())    // this is the part i'm having trouble with................................
                        {
                            Console.WriteLine("{0}", p.Number.ToString());
                            Console.WriteLine("You can't pass it to yourself! -_- Choose someone else..." + p.Number.ToString());
                            Console.ReadKey();
                        }
                    }
                }
            } while (NumOfRounds != 0);
            if(Player1.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player1.Name);
                Console.ReadKey();
                Main();
            }
            else if(Player2.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player2.Name);
                Console.ReadKey();
                Main();
            }
            else if (Player3.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player3.Name);
                Console.ReadKey();
                Main();
            }
            else if (Player4.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player4.Name);
                Console.ReadKey();
                Main();
            }
        }
    }        

    class Player
    {
        public string Name { get; set; }
        public int Number { get; set; }
        public bool HasPotatoe { get; set; }
        public bool IsOut { get; set; }
    }
}

错误的具体部分:

Random rand = new Random();
            int NumOfRounds = rand.Next(1, 10);
            do
            {
                foreach (Player p in PlayingList)
                {
                    if (p.HasPotatoe == true)
                    {
                        Console.Write("Player {0} has the potatoe! Who do you want to pass it to? (by number): ", p.Number);
                        string input = Console.ReadLine();
                        if (input == "1")
                        {
                            PlayingList[0].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "2")
                        {
                            PlayingList[1].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "3")
                        {
                            if (Player3.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[2].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if (input == "4")
                        {
                            if (Player4.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[3].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if(input == p.Number.ToString())  // this is where the problem is...
                        {
                            Console.WriteLine("{0}", p.Number.ToString());
                            Console.WriteLine("You can't pass it to yourself! -_- Choose someone else..." + p.Number.ToString());
                            Console.ReadKey();
                        }
                    }
                }
            } while (NumOfRounds != 0);

基本上我说:if(input == p.Number()。ToString())// p.Number()== player&#39; s number 然后抛出异常。但我不确定该怎么做。我尝试使用.ToString()方法将整数转换为字符串。但它似乎没有改变任何东西。我也尝试使用Int32.Parse(输入)方法将输入转换为32位整数,但仍然没有运气。结果只是随机做了一些事情。例如,如果我是玩家二,并且我提示将马铃薯传递给玩家二,那么它只会写入控制台,玩家2一遍又一遍地吃马铃薯。我不确定如何做到这一点。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

抛出异常很简单。

  else if(input == p.Number.ToString())  
    throw new Exception("You cant do that");

也许您的问题是创建自己的异常以便以后捕获它们。做

    public class BadPlayerException: Exception
    {
    }

...

throw new BadPlayerEcxeption():