IF和其他语句异常处理帮助-其他消息不起作用

时间:2019-07-03 22:10:04

标签: c# if-statement exception

else语句不起作用,当控制台输入不是Rock,Paper或Scissors时,不会显示异常消息。是什么原因。

using System;

namespace Rock__Paper__Scissors_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
            Console.Write("Enter Rock, Paper or Scissors:");
            string userChoice = Console.ReadLine();

            Random r = new Random();
            int computerChoice = r.Next(3);

            //0 = Scissors
            if (computerChoice == 0)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose scissors!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Scissors!");
                    Console.WriteLine("You WIN!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Scissors");
                    Console.WriteLine("You LOSE!");

                }
            }

            //1 = Rock
            else if (computerChoice == 1)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Rock");
                    Console.WriteLine("You WIN!");
                }
            }

            //2 = Paper
            else if (computerChoice == 2)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You WIN");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Paper");
                    Console.WriteLine("TIE!");
                }
            }

            //3 = Exception Handling
            else
            {
                Console.WriteLine("You must enter Rock, Paper or Scissors");
            }


        }
    }
}

2 个答案:

答案 0 :(得分:0)

在继续操作之前检查值或userChoice ...我的偏好是使用while循环

using System;

namespace Rock__Paper__Scissors_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
            Console.Write("Enter Rock, Paper or Scissors:");
            string userChoice = Console.ReadLine();

            //Check it here in a while loop, until the user gets it 
            //right, the program will not proceed and loop here
            while (userChoice != "Scissors" || userChoice != "Rock" || userChoice != "Paper")
            {
                Console.Write("You must enter Rock, Paper or Scissors");
                userChoice = Console.ReadLine();
            }

            Random r = new Random();
            int computerChoice = r.Next(3);

            //0 = Scissors
            if (computerChoice == 0)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose scissors!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Scissors!");
                    Console.WriteLine("You WIN!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Scissors");
                    Console.WriteLine("You LOSE!");

                }
            }

            //1 = Rock
            else if (computerChoice == 1)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Rock");
                    Console.WriteLine("You WIN!");
                }
            }

            //2 = Paper
            else if (computerChoice == 2)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You WIN");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Paper");
                    Console.WriteLine("TIE!");
                }
            }
        }
    }
}

答案 1 :(得分:0)

添加了更多的if / if else语句,而不是else。现在它会吐出我想要的异常错误。

进行此操作的目的是练习/应用if,if,ifother等方法,因为我正在尝试学习c#并在线学习一些教程。当然,有更好的方法可以制作这款游戏​​。

-需要添加某种循环(当我学习如何做时)。 -计算机似乎会以可预测的顺序生成随机数,并且看起来并不是那么随机,因此我需要进行改进。

使用系统;

namespace Rock__Paper__Scissors_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
            Console.Write("Enter Rock, Paper or Scissors:");
            string userChoice = Console.ReadLine();

            Random r = new Random();
            int computerChoice = r.Next(2);


            //0 = Scissors
            if (computerChoice == 0)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose scissors!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Scissors!");
                    Console.WriteLine("You WIN!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Scissors");
                    Console.WriteLine("You LOSE!");

                }
            }

            //1 = Rock
            else if (computerChoice == 1)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Rock!");
                    Console.WriteLine("TIE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Rock");
                    Console.WriteLine("You WIN!");
                }
            }

            //2 = Paper
            else if (computerChoice == 2)
            {
                if (userChoice == "Scissors")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You WIN");
                }

                else if (userChoice == "Rock")
                {
                    Console.WriteLine("Computer chose Paper!");
                    Console.WriteLine("You LOSE!");
                }

                else if (userChoice == "Paper")
                {
                    Console.WriteLine("Computer chose Paper");
                    Console.WriteLine("TIE!");
                }
            }


            //Exception Handling
            if (userChoice != "Scissors")
            {
                Console.WriteLine("Choose Rock, Paper or Scissors");
            }

            else if (userChoice != "Rock")
            {
                Console.WriteLine("Choose Rock, Paper or Scissors");
            }

            else if (userChoice != "Paper")
            {
                Console.WriteLine("Choose Rock, Paper or Scissors");
            }


        }
    }
}