【问题标题】:Operator `+' cannot be applied to operands of type `int' and `System.Random'运算符“+”不能应用于“int”和“System.Random”类型的操作数
【发布时间】:2022-01-07 08:23:26
【问题描述】:

我是 c# 的初学者,所以我试图制作一个程序,为你和敌人掷骰子 10 回合,每回合将你的掷骰数加到总计数中,谁得到最大最后赢了,我没有一路完成,但这是我目前所拥有的:

namespace dfgs
{
   class dice
   {
       public static void Main(String[] args)
       {
           int plsc = 0;
           int aisc = 0;
           int turns = 0;
           Random plrnd = new Random();
           Random airnd = new Random();
           while (turns < 11)
            {
                Console.WriteLine("Player Roll Is" + Convert.ToInt32(plrnd.Next(6)));
                Console.WriteLine("AI Roll Is" + Convert.ToInt32(airnd.Next(6)));
                plsc = plsc + plrnd;
                aisc = aisc + airnd;
                Console.WriteLine("Type A and hit enter to go again");
                string nxt = Console.ReadLine();
                if (nxt == "A"){
                    turns++;
                }
                Console.ReadLine();
            }
            
       }   
   } 
}

每当我尝试编译时,我都会收到错误Operator +' cannot be applied to operands of type int' and System.Random',此错误出现两次,我尝试将随机数的类型更改为 int 但随后我收到错误消息Type int' does not contain a definition for Next' and no extension method Next' of type int' could be found. Are you missing an assembly reference? 我有点卡在这里,任何帮助将不胜感激。

编辑:感谢所有回答的人,我已经成功完成了这项工作,这是最终代码,它不是最干净的,但可以按预期工作:

namespace dfgs
{
   class die
   {
       public static void Main(String[] args)
       {
           int plsc = 0;
           int aisc = 0;
           int turns = 0;
           Random plrnd = new Random();
           Random airnd = new Random();
           while (turns < 10)
            {
                Console.WriteLine("Player Roll Is " + Convert.ToInt32(plrnd.Next(6)));
                Console.WriteLine("AI Roll Is " + Convert.ToInt32(airnd.Next(6)));
                plsc = plsc + plrnd.Next(6);
                aisc = aisc + airnd.Next(6);
                Console.WriteLine("Type A and hit enter to go again");
                string nxt = Console.ReadLine();
                if (nxt == "A"){
                    turns++;
                }
                else{
                    break;
                }
                if (turns == 10){
                    if (plsc > aisc){
                        Console.WriteLine("The Player Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
                    }

                    else if (aisc > plsc){
                        Console.WriteLine("The AI Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
                    }
                    break;
                    
                }
               
            }
            Console.ReadLine();
       }   
   } 
}

【问题讨论】:

  • 您忘记在plrndairnd 之后放置.Next()
  • 您真的不需要多个Random 实例。使用相同的实例来获取所有随机数。正如您设置的那样,plrndairnd 可能会发出相同的随机数序列(这可能不是您想要的)。另一个专业提示:将这些变量称为 playerRandom(或 playerRandplayerRng(其中 RNG 是随机数生成器))只需要一些额外的击键,但会让您的代码在下次查看时更有意义在它:“我想知道我所说的 air-ND 是什么意思”
  • 搜索标题结果this post

标签: c# random


【解决方案1】:

看看这一行:

plsc = plsc + plrnd;

在此代码中,plrnd 不是数字。相反,它是生成数字的生成器。你必须告诉生成器给你下一个数字,就像你在上面那行所做的那样:

plrnd.Next(6)

此外,您可能需要更改上面的代码以将每次调用的结果保存在变量中,这样您就可以使用相同的值向用户显示并增加总计数。

【讨论】:

  • 非常感谢!
【解决方案2】:

您可能希望保存从 Random 获得的随机 int,而不是尝试计算数字,再加上 random_number_generator_machine - 在某些现实世界中,这就像问某人“你的年龄是多少,除以奶酪三明治?”..

            var plroll = plrnd.Next(6);
            var airoll = airnd.Next(6);
            Console.WriteLine("Player Roll Is" + plroll);
            Console.WriteLine("AI Roll Is" + airoll));
            plsc = plsc + plroll;
            aisc = aisc + airoll;

您不需要播放器的 Random 和计算机的 Random;一个 Random 可以为两者充分生成,顺便说一句:

       int plsc = 0;
       int aisc = 0;
       int turns = 0;
       Random r = new Random();
       while (turns < 11)
       {
            var plroll = r.Next(6);
            var airoll = r.Next(6);
            Console.WriteLine("Player Roll Is" + plroll);
            Console.WriteLine("AI Roll Is" + airoll));
            plsc = plsc + plroll;
            aisc = aisc + airoll;

【讨论】:

  • 感谢您的帮助。如果我只使用一个随机数,在我看来,区分玩家分数和 AI 分数会做更多工作,但我可能是错的。
  • 我做了一个编辑来演示只需要一个 Random
【解决方案3】:

由于您想多次使用滚动的结果,开始将结果分配给变量以供以后使用:

while (turns < 11)
{
    // roll dies, store results in local variable
    var plRoll = plrnd.Next(6);
    var aiRoll = airnd.Next(6);

    // then reuse the variables
    Console.WriteLine("Player Roll Is " + plRoll);
    Console.WriteLine("AI Roll Is " + aiRoll);
    plsc = plsc + plRoll;
    aisc = aisc + aiRoll;
    Console.WriteLine("Type A and hit enter to go again");
    string nxt = Console.ReadLine();
    if (nxt == "A"){
        turns++;
    }
    else {
        // remember to break out if the player doesn't want to continue
        break;
    }
}

【讨论】:

    【解决方案4】:

    int 是 C# 中的内置值数据类型。 Random 是一个类对象。 + 运算符不知道如何将int 数据类型和Random 类对象相加。您可能打算使用Next() 方法,确实返回int。此外,您只想将new 升级到Random 课程一次。考虑改用此代码:

    namespace dfgs
    {
       class dice
       {
           public static void Main(String[] args)
           {
               Random rnd = new Random();
               int plsc = 0;
               int aisc = 0;
               int turns = 0;
               int plrnd = rnd.Next(6);
               int airnd = rnd.Next(6);
              while (turns < 11)
                {
                    Console.WriteLine("Player Roll Is" + plrnd);
                    Console.WriteLine("AI Roll Is" + airnd);
                    plsc = plsc + plrnd;
                    aisc = aisc + airnd;
                    Console.WriteLine("Type A and hit enter to go again");
                    string nxt = Console.ReadLine();
                    if (nxt == "A"){
                        turns++;
                    }
                    Console.ReadLine();
                }
            
           }   
       } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多