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



else语句不起作用,当控制台的输入不是岩石,纸或剪刀时,异常消息未显示。

的原因是什么。
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");
            }

        }
    }
}

在继续之前检查值或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!");
                }
            }
        }
    }
}

如果/if其他语句而不是其他,则添加了更多。现在,它吐出了我想要的异常错误。

进行此操作的目的是练习/申请,如果我想学习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");
            }

        }
    }
}

相关内容

最新更新