【问题标题】:Unable to cast object of type 'Dice' to type 'System.IConvertible'无法将“骰子”类型的对象转换为“System.IConvertible”类型
【发布时间】:2019-03-11 17:52:58
【问题描述】:

我正在尝试做一个骰子游戏,但由于某种原因发生了这个错误。

无法将“Dice”类型的对象转换为“System.IConvertible”类型

这是我的代码:

class Dice
{
    int result;

    public void DiceRoll()
    {
        Random rnd = new Random();
        result = rnd.Next(1, 7);
     }   
}

Console.WriteLine("Player 1 Turn" + roll);
int enterscore1 = Convert.ToInt32(roll);

Console.WriteLine("Player 2 Turn" + roll);
int enterscore2 = Convert.ToInt32(roll);

【问题讨论】:

  • 您得到的确切错误是什么?以及如何宣布滚动?
  • 什么是roll变量?
  • 我假设 roll = new Dice() 但我不明白创建 Dice 对象的意义。只需在代码中的某处使用static Random rnd = new Random();,然后在需要新骰子时调用rnd.Next(1,7)

标签: c#


【解决方案1】:

一些注意事项:

  • 当从用户获取输入(即作为文本)并想要获取其数值(作为int 值)。

    这里有一个 Dice 类的骨架,它已经与 int 一起使用,所以 不需要转换

  • 我会强烈建议您不要像消息所暗示的那样尝试实施 IConvertible - 这并不是它的真正目的。

  • 请注意,对于像您这样的情况,强烈建议您使用 Random 的单个实例,因为 constructor

    使用与时间相关的默认种子值。

    因此快速连续创建多个Random 实例可能会使用相同的种子。如果你这样做,因为Random 类上的方法不是线程安全的,你应该在某个对象上lock 以防止多个线程同时调用它的方法(如下所示)。


有几种方法可以做你想做的事。以下是一些:

  1. 拥有Dice 的单个实例并滚动多次:

    public class Dice
    {
        private static readonly Random random = new Random();
    
        public int Result
        {
            get;
            private set;
        }
    
        public void Roll()
        {
            lock ( random )
                Result = random.Next(1, 7);
        }
    }
    
    public static void Main()
    {
        var dice = new Dice();
    
        dice.Roll();
        var player1Result = dice.Result;
        Console.WriteLine("Player 1 rolls: " + player1Result);
    
        dice.Roll();
        var player2Result = dice.Result;
        Console.WriteLine("Player 2 rolls: " + player2Result);
    }
    

    (试试看here

    请注意,我已为每个掷骰分配了一个变量 - 这使您可以使用 >< compare 结果来找出谁获胜。

  2. 为每个玩家创建一个单独的Dice 实例:

    public class Dice
    {
        private static readonly Random random = new Random();
    
        private readonly int _minValue;
        private readonly int _maxValue;
    
        public Dice(int minValue, int maxValue)
        {
            _minValue = minValue;
            _maxValue = maxValue;
        }
    
        public int Result
        {
            get;
            private set;
        }
    
        public void Roll()
        {
            lock ( random )
                Result = random.Next(_minValue, _maxValue + 1);
        }
    }
    
    public static void Main()
    {
        var player1Dice = new Dice(1, 6);
        player1Dice.Roll();
        var player1Result = player1Dice.Result;
        Console.WriteLine("Player 1 rolls: " + player1Result);
    
        var player2Dice = new Dice(1, 5);
        player2Dice.Roll();
        var player2Result = player2Dice.Result;
        Console.WriteLine("Player 2 rolls: " + player2Result);
    }
    

    (试试看here

    您仍然可以进行比较,但是因为您有不同的 Dice 实例,您可以将自己的构造函数添加到 Dice,例如,将赔率叠加到有利于一位玩家(这里我做了第二个玩家的骰子只能掷出 1-5)。

  3. 只要有一个静态的Dice

    public static class Dice
    {
        private static readonly Random random = new Random();
    
        public static int Roll()
        {
            lock ( random )
                return random.Next(1, 7);
        }
    }
    
    public static void Main()
    {
        var player1Result = Dice.Roll();
        Console.WriteLine("Player 1 rolls: " + player1Result);
    
        var player2Result = Dice.Roll();
        Console.WriteLine("Player 2 rolls: " + player2Result);
    }
    

    (试试看here

    如果您永远不会对 Dice 做任何花哨的事情,并且预计它不会有任何状态(例如滚动不同的值,或了解以前的滚动)并且您需要有一个单独的班级,这就是我的做法。您不需要构造函数,因为Dicestatic。同样,您可以对这两个结果进行比较。

【讨论】:

  • 随机不是线程安全的。因此,如果您在两个不同的线程(或更多)中创建两个不同的骰子类,您可能会得到非常奇怪的结果。如果您要使用静态随机数,则需要以线程安全的方式访问它。
【解决方案2】:

如果你将 roll 声明为 Dice 类的变量,你的代码应该是

int enterscore1 = Convert.ToInt32(roll.result);

Console.WriteLine("Player 2 Turn" + roll);
int enterscore2 = Convert.ToInt32(roll.result);

无法将“Dice”类型的对象转换为“System.IConvertible”类型

这个错误意味着你放置的参数没有 IConvertible 接口,这对于 Convert 类方法是必不可少的。

【讨论】:

    【解决方案3】:

    您需要包含int enterscore1 = DiceRoll();,这将有助于在掷骰子方法中将随机变量定义为整数

    【讨论】:

    • 好吧,DiceRoll() 的返回类型为 void,所以这行不通:)
    • 你可以在其中添加 int 而不是 void
    猜你喜欢
    • 2015-12-17
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多