本文介绍了我需要帮助,以了解如何解决此代码...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

命名空间ConsoleApplication5
{
   课程计划
    {
       枚举rpsFormat//枚举剪刀石头布的选项
        {
           岩石= 1,纸,剪刀
        }
       静态void Main(string [] args)
        {
            int userInput,compInput,结果,尝试,inputCast,compCast;
           尝试次数= 5;
            for(int i = 0; i<尝试; i ++)
            Console.WriteLine(您有{0}次尝试",尝试);
            Console.Write(输入1表示岩石,2表示纸张,3表示剪刀.");
            userInput = Convert.ToInt32(Console.ReadLine());
           随机ranNumberGenerator = new Random();  //生成随机数
            compInput = ranNumberGenerator.Next(1,4); //介于1和3之间
           结果=(userInput-compInput + 10)-((userInput-compInput + 10)/3)* 3;

            int inputCast = Convert.ToInt32(userInput);
            int compCast = Convert.ToInt32(compInput);

               开关(结果)
                {
                   案例0://loss
                        Console.WriteLine(您丢了...");
                        Console.WriteLine(用户选择的{0}",(rpsFormat(inputCast)));
                        Console.WriteLine(选定的计算机{0}"(,(rpsFormat(compCast))));
                       休息;

                   情况1://draw
                        Console.WriteLine("Its Draw ...");
                        Console.WriteLine(用户选择的{0}",(rpsFormat(inputCast)));
                        Console.WriteLine(选定的计算机{0}"(,(rpsFormat(compCast))));
                       休息;

                   情况2://win
                        Console.WriteLine(" You Won !!!"));
                        Console.WriteLine(用户选择的{0}",(rpsFormat(inputCast)));
                        Console.WriteLine(所选计算机{0}"(,(rpsFormat(compCast))));
                       休息;

                   默认值://输入无效
                        Console.WriteLine(无效输入...");
                       休息;
                }

            Console.ReadLine();
        }
    }
}

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

namespace ConsoleApplication5
{
    class Program
    {
        enum rpsFormat // enumerate the options for rock paper scissors
        {
            Rock = 1, Paper, Scissors
        }
        static void Main(string[] args)
        {
            int userInput, compInput, result, attempts, inputCast, compCast;
            attempts = 5;
            for (int i = 0; i < attempts; i++)
            Console.WriteLine("You have {0} Attempts", attempts);
            Console.Write("Input 1 for rock, 2 for paper, 3 for scissors. ");
            userInput = Convert.ToInt32(Console.ReadLine());
            Random ranNumberGenerator = new Random();  //generate a random number
            compInput = ranNumberGenerator.Next(1, 4); //between 1 and 3
            result = (userInput - compInput + 10) - ((userInput - compInput + 10) / 3) * 3;

            int inputCast = Convert.ToInt32(userInput);
            int compCast = Convert.ToInt32(compInput);

                switch (result)
                {
                    case 0: //loss
                        Console.WriteLine("You Lost...");
                        Console.WriteLine("User selected {0}", (rpsFormat(inputCast)));
                        Console.WriteLine("Computer Selected {0}", (rpsFormat(compCast)));
                        break;

                    case 1: //draw
                        Console.WriteLine("Its a Draw...");
                        Console.WriteLine("User selected {0}", (rpsFormat(inputCast)));
                        Console.WriteLine("Computer Selected {0}", (rpsFormat(compCast)));
                        break;

                    case 2: //win
                        Console.WriteLine("You Won!!!");
                        Console.WriteLine("User selected {0}", (rpsFormat(inputCast)));
                        Console.WriteLine("Computer Selected {0}", (rpsFormat(compCast)));
                        break;

                    default: //invalid input
                        Console.WriteLine("invalid input...");
                        break;
                }

            Console.ReadLine();
        }
    }
}

调试器告诉我,枚举"rpsFormat"被视为一种方法.但是我不明白如何编写另一种方法来返回转换为字符串的整数(1到Rock ...等). 

The debugger is telling me that the enum 'rpsFormat' is being treated like a method. But i do not understand how to code another method to return a integer translated to a string, (1 to Rock...etc). 

出于这个原因,我什至都不真正了解如何编写另一种方法....

And for that matter i don't even really understand how to code another method.... 

请帮助?

推荐答案

代码中有一些小问题,

1.枚举从int转换为字符串
2.变量声明.同一变量多次声明
3.代码必须包含在for循环中才能多次尝试.该代码没有带有for循环的花括号,这就是为什么它显示单次尝试.

1. enum casting from int to string
2. Variable declaration. same variable declare more than once
3. Code must be include in for loop for multiple attempt. The code doesn't have curly braces with for loop that's why it shows single attempt.

 请查看下面的更新代码,其中所有三个问题均已修复.

 Please have a look on below updated code in which all three issues are fixed .

using System;

namespace DevApp
{
    public class Program
    {
        enum rpsFormat // enumerate the options for rock paper scissors
        {
            Rock = 1, Paper, Scissors
        }
        static void Main(string[] args)
        {
            int userInput, compInput, result, attempts, inputCast, compCast;
            attempts = 5;
            for (int i = 0; i < attempts; i++)
            {
                Console.WriteLine("You have {0} Attempts", attempts);

                Console.Write("Input 1 for rock, 2 for paper, 3 for scissors. ");
                userInput = Convert.ToInt32(Console.ReadLine());
                Random ranNumberGenerator = new Random();  //generate a random number
                compInput = ranNumberGenerator.Next(1, 4); //between 1 and 3
                result = (userInput - compInput + 10) - ((userInput - compInput + 10) / 3) * 3;

                inputCast = Convert.ToInt32(userInput);
                compCast = Convert.ToInt32(compInput);

                switch (result)
                {
                    case 0: //loss
                        Console.WriteLine("You Lost...");
                        Console.WriteLine("User selected {0}", ((rpsFormat)inputCast).ToString());
                        Console.WriteLine("Computer Selected {0}", ((rpsFormat)compCast).ToString());
                        break;

                    case 1: //draw
                        Console.WriteLine("Its a Draw...");
                        Console.WriteLine("User selected {0}", ((rpsFormat)inputCast).ToString());
                        Console.WriteLine("Computer Selected {0}", ((rpsFormat)compCast).ToString());
                        break;

                    case 2: //win
                        Console.WriteLine("You Won!!!");
                        Console.WriteLine("User selected {0}", ((rpsFormat)inputCast).ToString());
                        Console.WriteLine("Computer Selected {0}", ((rpsFormat)compCast).ToString());
                        break;

                    default: //invalid input
                        Console.WriteLine("invalid input...");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}


这篇关于我需要帮助,以了解如何解决此代码...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:50