You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#编写掷骰子游戏报运算符+无法应用于int和System.Random类型如何解决

C# 10回合制掷骰子对战游戏开发问题解决

需求说明

C#初学者计划开发一款10回合制掷骰子对战游戏,规则如下:

  • 每回合分别生成玩家和AI的掷骰点数,累加到各自总得分中
  • 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();
            }
            
       }   
   } 
}

报错信息

编译时触发两次报错:

  1. Operator +' cannot be applied to operands of type int' and System.Random'
  2. 尝试将随机数变量类型改为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?

错误原因

  • 第一个报错核心:plrnd和airnd是Random类的实例,作用是生成随机数,本身不是数值,不能直接和int类型的得分变量相加
  • 第二个报错核心:Random类才有Next()方法用来生成随机数,把变量改成int类型后自然不存在Next()方法,属于错误修改方向

修复后可正常运行的代码

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

额外优化建议

如果要模拟常规1-6点的骰子,可将所有plrnd.Next(6)、airnd.Next(6)修改为plrnd.Next(1,7)、airnd.Next(1,7);另外如果要保证控制台打印的掷骰点数和累加到得分的点数一致,可以先将生成的随机数存到临时变量中,再分别用于打印和累加,避免两次调用Next生成不同的数值。

内容的提问来源于stack exchange,提问作者thenetwork

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:06:08