运算符“+”不能应用于“int”和“System.Random”类型的操作数

Operator `+' cannot be applied to operands of type `int' and `System.Random'

我是 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? i我有点卡在这里,任何帮助将不胜感激。

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

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();
       }   
   } 
}

看看这一行:

plsc = plsc + plrnd;

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

plrnd.Next(6)

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

您可能想要保存从 Random 中获得的随机整数,而不是尝试计算数字,再加上一个 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 可以为两者充分生成,顺便说一句:

       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;

由于您想多次使用掷骰结果,开始通过将结果分配给变量供以后使用:

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;
    }
}

int 是 C# 中的内置值数据类型。 Random 是一个 class 对象。 + 运算符不知道如何将 int 数据类型和 Random class 对象相加。您可能想做的是使用 Next() 方法,该方法 执行 return 和 int。此外,您只想 new 向上 Random class 一次。考虑改用此代码:

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();
            }
        
       }   
   } 
}