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



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

遇到以下问题:我正在初始化每个单独的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;
}
在没有故意添加第二个问题的情况下,我想将这个botask列表返回给我的Robot类。所以在用户创建了一个Robot之后,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;
}
}
}
}

事先感谢任何帮助-谢谢。

不能为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;
}

相关内容

  • 没有找到相关文章

最新更新