第二个用户输入的验证无法正常工作,我被告知我有很多不必要的编码

发布于 2024-12-11 21:48:09 字数 5518 浏览 0 评论 0原文

这是我学习计算机编程课程的第一年,我当前的任务之一是创建一个可行的石头剪刀布游戏。

我已经完成了大部分工作,但我的老师说我有很多不必要的代码,但没有真正告诉我它们是什么。我希望尽可能地简化流程,因为我不仅仅满足于任何有效的方法。

同样,用户输入的第二次验证(用户做出选择,程序运行并显示谁获胜,然后被要求再次做出选择)不像第一次那样工作,我无法弄清楚出了什么问题。过去两周我一直在研究这个问题,但无法弄清楚,因此非常感谢您的帮助。

我的代码如下:

namespace Assignment04
{
class Program
{
    static void Main(string[] args)
    {
        // Declare variables
        int gamesPlayed = 0, userWins = 0, computerWins = 0, draws = 0, userSelection, computerSelection;
        bool inputIsValid = false;
        // Prompt user
        Console.WriteLine("Welcome to the Rock, Paper, Scissors game!");
        Console.WriteLine("1 - Rock");
        Console.WriteLine("2 - Paper");
        Console.WriteLine("3 - Scissors");
        Console.WriteLine("4 - Quit program and view record");
        // Create a loop to validate user's selection
            do
            {
                Console.Write("Please make a selection: ");
                // loop and test using TryParse()
                while (!int.TryParse(Console.ReadLine(), out userSelection))
                {
                    // invalid data type
                    Console.WriteLine("Invalid input.");
                    Console.Write("Please make a selection: ");
                }
                // test if input is within acceptable range
                if (userSelection >= 1 && userSelection <= 4)
                {
                    inputIsValid = true;
                }
                else
                {
                    // valid integer, but out of acceptable range
                    Console.WriteLine("Number out of range.");
                }
            } while (!inputIsValid);
            // Display user's choice
            while (userSelection >= 1 && userSelection <= 3)
            {
                if (userSelection == 1)
                {
                    Console.WriteLine("\nYou have selected Rock");
                    gamesPlayed = gamesPlayed + 1;
                }
                else if (userSelection == 2)
                {
                    Console.WriteLine("\nYou have selected Paper");
                    gamesPlayed = gamesPlayed + 1;
                }
                else if (userSelection == 3)
                {
                    Console.WriteLine("\nYou have selected Scissors");
                    gamesPlayed = gamesPlayed + 1;
                }
                // Generate computer's choice
                Random randomNumber = new Random();
                computerSelection = randomNumber.Next(1, 4);
                // Display computer's choice
                if (computerSelection == 1)
                {
                    Console.WriteLine("Computer has chosen Rock");
                }
                else if (computerSelection == 2)
                {
                    Console.WriteLine("Computer has chosen Paper");
                }
                else if (computerSelection == 3)
                {
                    Console.WriteLine("Computer has chosen Scissors");
                }
                // Calculate and display who wins
                if (userSelection == computerSelection)
                {
                    draws = draws + 1;
                }
                else if (userSelection == 1 && computerSelection == 3 || userSelection == 2 && computerSelection == 1 || userSelection == 3 && computerSelection == 2)
                {
                    userWins = userWins + 1;
                }
                else if (userSelection == 1 && computerSelection == 2 || userSelection == 2 && computerSelection == 3 || userSelection == 3 && computerSelection == 1)
                {
                    computerWins = computerWins + 1;
                }
                // Display results and statistics
                Console.WriteLine("\nYou have played {0} games with {1} wins, {2} draws, and {3} losses.", gamesPlayed, userWins, draws, computerWins);
                do
                {
                    Console.Write("Please make a selection: ");
                    // loop and test using TryParse()
                    while (!int.TryParse(Console.ReadLine(), out userSelection))
                    {
                        // invalid data type
                        Console.WriteLine("Invalid input.");
                        Console.Write("Please make a selection: ");
                    }
                    // test if input is within acceptable range
                    if (userSelection >= 1 && userSelection <= 4)
                    {
                        inputIsValid = true;
                    }
                    else
                    {
                        // valid integer, but out of acceptable range
                        Console.WriteLine("Number out of range.");
                    }
                } while (!inputIsValid);
            }
            if (gamesPlayed == 0 && userSelection == 4)
            {
                Console.WriteLine("\nGoodbye");
            }
            else if (gamesPlayed > 0 && userSelection == 4)
            {
                Console.WriteLine("\nGames played = " + gamesPlayed);
                Console.WriteLine("User wins = " + userWins);
                Console.WriteLine("Computer wins = " + computerWins);
                Console.WriteLine("Draws = " + draws);
            }
        Console.ReadLine();
    }
}
}

This is my first year in a Computer Programming course and one of my current assignments is to create a working Rock, Paper, Scissors game.

I have most of it working but my teacher has said that I have a lot of unnecessary code without actually telling me what it is. I'd like to get this streamline as best as possible as I'm not just satisfied with whatever works.

As well, the second validation of user input (user makes a choice, program runs through and displays who wins then is asked to make a choice again) doesn't work like the first one and I can't figure out what's going wrong. I've been looking at this for the past 2 weeks and can't figure it out so any help is appreciated.

The code that I have is as follows:

namespace Assignment04
{
class Program
{
    static void Main(string[] args)
    {
        // Declare variables
        int gamesPlayed = 0, userWins = 0, computerWins = 0, draws = 0, userSelection, computerSelection;
        bool inputIsValid = false;
        // Prompt user
        Console.WriteLine("Welcome to the Rock, Paper, Scissors game!");
        Console.WriteLine("1 - Rock");
        Console.WriteLine("2 - Paper");
        Console.WriteLine("3 - Scissors");
        Console.WriteLine("4 - Quit program and view record");
        // Create a loop to validate user's selection
            do
            {
                Console.Write("Please make a selection: ");
                // loop and test using TryParse()
                while (!int.TryParse(Console.ReadLine(), out userSelection))
                {
                    // invalid data type
                    Console.WriteLine("Invalid input.");
                    Console.Write("Please make a selection: ");
                }
                // test if input is within acceptable range
                if (userSelection >= 1 && userSelection <= 4)
                {
                    inputIsValid = true;
                }
                else
                {
                    // valid integer, but out of acceptable range
                    Console.WriteLine("Number out of range.");
                }
            } while (!inputIsValid);
            // Display user's choice
            while (userSelection >= 1 && userSelection <= 3)
            {
                if (userSelection == 1)
                {
                    Console.WriteLine("\nYou have selected Rock");
                    gamesPlayed = gamesPlayed + 1;
                }
                else if (userSelection == 2)
                {
                    Console.WriteLine("\nYou have selected Paper");
                    gamesPlayed = gamesPlayed + 1;
                }
                else if (userSelection == 3)
                {
                    Console.WriteLine("\nYou have selected Scissors");
                    gamesPlayed = gamesPlayed + 1;
                }
                // Generate computer's choice
                Random randomNumber = new Random();
                computerSelection = randomNumber.Next(1, 4);
                // Display computer's choice
                if (computerSelection == 1)
                {
                    Console.WriteLine("Computer has chosen Rock");
                }
                else if (computerSelection == 2)
                {
                    Console.WriteLine("Computer has chosen Paper");
                }
                else if (computerSelection == 3)
                {
                    Console.WriteLine("Computer has chosen Scissors");
                }
                // Calculate and display who wins
                if (userSelection == computerSelection)
                {
                    draws = draws + 1;
                }
                else if (userSelection == 1 && computerSelection == 3 || userSelection == 2 && computerSelection == 1 || userSelection == 3 && computerSelection == 2)
                {
                    userWins = userWins + 1;
                }
                else if (userSelection == 1 && computerSelection == 2 || userSelection == 2 && computerSelection == 3 || userSelection == 3 && computerSelection == 1)
                {
                    computerWins = computerWins + 1;
                }
                // Display results and statistics
                Console.WriteLine("\nYou have played {0} games with {1} wins, {2} draws, and {3} losses.", gamesPlayed, userWins, draws, computerWins);
                do
                {
                    Console.Write("Please make a selection: ");
                    // loop and test using TryParse()
                    while (!int.TryParse(Console.ReadLine(), out userSelection))
                    {
                        // invalid data type
                        Console.WriteLine("Invalid input.");
                        Console.Write("Please make a selection: ");
                    }
                    // test if input is within acceptable range
                    if (userSelection >= 1 && userSelection <= 4)
                    {
                        inputIsValid = true;
                    }
                    else
                    {
                        // valid integer, but out of acceptable range
                        Console.WriteLine("Number out of range.");
                    }
                } while (!inputIsValid);
            }
            if (gamesPlayed == 0 && userSelection == 4)
            {
                Console.WriteLine("\nGoodbye");
            }
            else if (gamesPlayed > 0 && userSelection == 4)
            {
                Console.WriteLine("\nGames played = " + gamesPlayed);
                Console.WriteLine("User wins = " + userWins);
                Console.WriteLine("Computer wins = " + computerWins);
                Console.WriteLine("Draws = " + draws);
            }
        Console.ReadLine();
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-12-18 21:48:09

如果您发现自己多次编写几乎相同的内容,则应该寻找重构它的方法。下面是一个不必要的重复代码的示例:

if (userSelection == 1)
{
    Console.WriteLine("\nYou have selected Rock");
    gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
    Console.WriteLine("\nYou have selected Paper");
    gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
    Console.WriteLine("\nYou have selected Scissors");
    gamesPlayed = gamesPlayed + 1;
}

可以简化为:

Console.WriteLine("\nYou have selected " + tools[userSelection - 1]);
gamesPlayed++;

其中工具定义为:

string[] tools = { "Rock", "Paper", "Scissors" };

If you find yourself writing almost the same thing multiple times, you should look for ways to refactor it. Here's an example of unnecessary repeated code:

if (userSelection == 1)
{
    Console.WriteLine("\nYou have selected Rock");
    gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 2)
{
    Console.WriteLine("\nYou have selected Paper");
    gamesPlayed = gamesPlayed + 1;
}
else if (userSelection == 3)
{
    Console.WriteLine("\nYou have selected Scissors");
    gamesPlayed = gamesPlayed + 1;
}

Can be simplified to:

Console.WriteLine("\nYou have selected " + tools[userSelection - 1]);
gamesPlayed++;

Where tools is defined as:

string[] tools = { "Rock", "Paper", "Scissors" };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文