如何计算和显示此岩石,纸张,剪刀应用的游戏结果 [英] How do I count and display the game results for this rock, paper, scissors application

查看:63
本文介绍了如何计算和显示此岩石,纸张,剪刀应用的游戏结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此应用程序时,它只显示请选择摇滚,纸张或剪刀,其次数与用户输入的numPlays相同。我还需要将结果存储在一个数组中,并在结尾显示所有结果。有人可以指点我正确的方向吗?



我尝试过:



主类:



When i run this application, it just displays "Please choose rock, paper or scissors" for as many times as the user inputs for the numPlays. I also need to store the results in an array and display all of the outcomes at the end. Can someone please point me in the right direction?

What I have tried:

Main Class:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Game rps = new Game();
            rps.start();
            rps.gameStart();
        }
    }
}









游戏类:







Game Class:

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

namespace ConsoleApplication1
{
    class Game
    {
        string name;
        int numPlays;
        int game;

        public void start()
        {
            Console.WriteLine("Welcome to rock, paper, scissors");
            this.userSettings();
        }

        public void userSettings()
        {
            Console.WriteLine("What is your name: ");
            name = Console.ReadLine();

            Console.WriteLine("How many games would you like to play?: ");
            Int32.TryParse(Console.ReadLine(), out numPlays);
            if (numPlays < 10)
            {
                while (numPlays % 2 == 0)
                {
                    Console.WriteLine("\nNumber is not odd try again.");
                    Console.WriteLine("How many games would you like to play?: ");
                    Int32.TryParse(Console.ReadLine(), out numPlays);
                }

                Console.ReadLine();
            }

            else
            {
                Console.WriteLine("Please insert number less than or equal to 9");
                this.userSettings();
            }


        }

        public void gameStart()
        {

        for(game = 1; game <= numPlays; game++)

            Console.WriteLine("Please choose Rock, Paper, or Scissors");
            string userSelection = Console.ReadLine();

            Random r = new Random();
            int computerSelection = r.Next(4);

            if (computerSelection == 1)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("Computer Choice: Rock\n");
                    Console.WriteLine("This round is a tie");
                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("Computer Choice: Paper\n");
                    Console.WriteLine("This round is a tie");
                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("Computer Choice: Scissors\n");
                    Console.WriteLine("This round is a tie");
                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");
                }
                Console.ReadLine();
            }

            else if (computerSelection == 2)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("Computer Choice: Paper\n");
                    Console.WriteLine("You lose this round");

                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("Computer Choice: Scissors\n");
                    Console.WriteLine("You lose this round");

                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("Computer Choice: Rock\n");
                    Console.WriteLine("You lose this round");
                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");
                }
                Console.ReadLine();
            }
            

            else if (computerSelection == 3)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("The computer chose scissors");
                    Console.WriteLine("You win this round, rock beats scissors");

                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("The computer chose rock");
                    Console.WriteLine("You win this round,paper beats rock");

                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("The computer chose paper");
                    Console.WriteLine("You win this round, scissors beats paper!");

                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");

                }
                Console.ReadLine();
            }
        }
    }
}

推荐答案

学习编程就像学习开车。当您学习如何驾驶汽车时,无论如何,您都需要在将手放在方向盘之前学习所有路标。编程是一样的,在开始你自己的项目之前你需要学习一套技术。



我的建议:

- 暂时忘掉这个项目。



- 通过阅读文档学习语言。



- 关注tutos熟悉常用技巧和概念。



- 尽快学习调试器,它是学习代码行为的好工具。

调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]



- 学习一些分析方法/技巧, Dijkstra自上而下方法是一个好的开始。

https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki /Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ ewd03xx / EWD316.PDF [ ^ ]



记住tutos的练习和小项目不是在这里做一些有用的东西,他们在这里教你编程。



建议:添加一个计算机选择显示,你会发现你的程序是完全错误的。

Learning programming is like learning to drive a car. When you learn how to drive a car, you are required to learn all road signs before putting your hands on the wheel, no matter what. It is the same for programming, there is a set of techniques you need to learn before starting your own projects.

My advices:
- Forget this project for now.

- Learn the language by reading documentation.

- Follow tutos to get familiar with common techniques and concepts.

- Learn the debugger as soon as possible, it is a great tool to learn how your code behave.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

- Learn some analyze methods/techniques, , Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

Remember the exercises and little projects of tutos are not here to make something useful, they are here to teach you programming.

Advice: add a display of computer choices and you will see that your program is completely wrong.
Random r = new Random();
int computerSelection = r.Next(4);
Console.WriteLine(computerSelection);
Console.WriteLine("\n");


这篇关于如何计算和显示此岩石,纸张,剪刀应用的游戏结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆