随机化 C# 中列表错误 CS1503 中对象的实例

时间:2021-02-27 21:37:29

标签: c# oop console-application

我正在用 C# 创建一个控制台应用程序。该应用程序的目的是创建机器人,创建机器人后随机分配 5 个任务,然后显示机器人的总时间等。我有一个用于 BotTask 和机器人的类。

遇到以下问题: 我正在初始化每个单独的 BotTask,然后尝试将它们加载到列表中,将它们随机化并返回 5 个可以分配给机器人的 BotTask。

我在想是否每个 BotTask 都有一个索引属性:我可以随机化一个整数,然后使用该索引属性来选择随机调用哪个 BotTask。

当我尝试此操作时,我收到错误代码 CS1503(论点 1:无法从“int”转换为“BotOMat.BotTask”)并且不确定我是否正在尝试以正确的方式完成此过程。

这是我为 BotTask 类编写的代码:

public class BotTask
{


    //initialize bot tasks
    public static readonly BotTask DISHES = new BotTask("Dishes", "Do the dishes", 1000, 1);
    public static readonly BotTask SWEEP = new BotTask("Sweep", "Sweep the house", 3000, 2);
    public static readonly BotTask LAUNDRY = new BotTask("Laundry", "Do the laundry", 10000, 3);
    public static readonly BotTask RECYCLING = new BotTask("Recycling", "Take out the recycling", 4000, 4);
    public static readonly BotTask SAMMICH = new BotTask("Sammich", "Make a sammich", 7000, 5);
    public static readonly BotTask LAWN = new BotTask("Lawn", "Mow the lawn", 20000, 6);
    public static readonly BotTask RAKE = new BotTask("Rake", "Rake the leaves", 18000, 7);
    public static readonly BotTask BATH = new BotTask("Bath", "Give the dog a bath", 14500, 8);
    public static readonly BotTask BAKE = new BotTask("Bake", "Bake some cookies", 8000, 9);
    public static readonly BotTask WASH = new BotTask("Wash", "Wash the car", 20000, 10);


    public string Name { get; private set; }
    public string Description { get; private set; }
    public int Duration { get; private set; }
    public int Index { get; private set; }


    private BotTask(string name, string description, int duration, int index)
    {
        Name = name;
        Description = description;
        Duration = duration;
        Index = index;
    }

    public static List<BotTask> randomizeBotTasks()
    {
        var loadBotTasks = new List<BotTask> { DISHES, SWEEP, LAUNDRY, RECYCLING, SAMMICH, LAWN, RAKE, BATH, BAKE, WASH };


        int i = 1;
        List<BotTask> randomizedBotTasks = new List<BotTask>();
        while (i <= 5)
        {
            var random = new Random();
            int index = random.Next(loadBotTasks.Count);
            randomizedBotTasks.Add(index);
            i++;
        }
        return randomizedBotTasks;
    }

这里没有故意添加第二个问题,我想将此 BotTasks 列表返回到我的机器人类。所以当用户创建一个机器人后,5 个随机的 BotTask 将分配给它。

这是我在我的机器人课程中尝试实现这一目标的方法:

    public class Robot
{

    public string BotName { get; set; }

    public RobotType BotType { get; set; }
    public string BotTypeDescription => BotType.GetDescription();

    public TimeSpan TimeElapsed { get; set; }

    private readonly List<BotTask> _tasks = new List<BotTask>();

    //public IEnumerable<BotTask> Tasks => _tasks;

    public Robot(string botName, RobotType botType, TimeSpan timeElapsed = default)
    {
        this.BotName = botName;
        this.BotType = botType;
        TimeElapsed = timeElapsed;
    }

        public static void CreateRobot()
    {

        var robotsList = new List<Robot>();
        //loop until there are no more robots
        while (true)
        {

            Console.Write("Enter robot name: ");
            var robotName = Console.ReadLine();
            if (string.IsNullOrEmpty(robotName))
            {
                break;  //empty robot, time to list things out
            }

            //RobotType? robotType = null;

            RobotType? robotType = null;
            while (!robotType.HasValue)
            {
                robotType = GetResponseUsingEnum<RobotType>("Robots");
            }

            var robot = new Robot(robotName, robotType.Value);
            robotsList.Add(robot);

        }

        //At this point, we have a fully populated list of robots, each with some tasks
        foreach (var robot in robotsList)
        {
            robot.Show();
        }
    }

    public static T? GetResponseUsingEnum<T>(string prompt) where T : struct, Enum
    {
        //Loop until a good answer (or no answer)
        while (true)
        {
            Console.WriteLine($"{prompt}: Please enter one of:");
            var values = (T[])Enum.GetValues(typeof(T));
            foreach (var enumValue in values)
            {
                var description = enumValue.GetDescription<T>();
                var intValue = Convert.ToInt32(enumValue);
                Console.WriteLine($"{intValue}: {description}");
            }
            Console.Write(">> ");
            var response = Console.ReadLine();
            if (string.IsNullOrEmpty(response))
            {
                return (T?)null;
            }
            if (Enum.TryParse<T>(response, out var val))
            {
                if (values.Contains(val))
                {
                    Console.WriteLine($"You answered: {val}");
                    return val;
                }
            }
        }
    }

提前感谢任何帮助 - 谢谢。

1 个答案:

答案 0 :(得分:0)

您不能将整数值添加到 List<BotTask> 使用这个:

  public static List<BotTask> randomizeBotTasks()
    {
        var loadBotTasks = new List<BotTask> { DISHES, SWEEP, LAUNDRY, RECYCLING, SAMMICH, LAWN, RAKE, BATH, BAKE, WASH };


        int i = 1;
        List<BotTask> randomizedBotTasks = new List<BotTask>();
        while (i <= 5)
        {
            var random = new Random();
            int index = random.Next(loadBotTasks.Count);
            randomizedBotTasks.Add(loadBotTasks[index]);
            i++;
        }
        return randomizedBotTasks;
    }
相关问题