C# C中列表错误CS1503中对象的随机化实例
我正在用c创建一个控制台应用程序。应用程序的目的是创建机器人,创建机器人后随机分配5个任务,然后显示机器人的总时间等。我有一个BotTask和robot类 有以下问题: 我正在初始化每个BotTask,然后尝试将它们加载到一个列表中,将它们随机化并返回5个可以分配给机器人的BotTask 我在想,如果每个BotTask都有一个index属性:我可以随机化一个整数,然后使用该index属性选择哪个BotTask被random调用 当我尝试此操作时,我收到错误代码CS1503参数1:无法从“int”转换为“BotOMat.BotTask”,并且不确定我是否尝试以正确的方式完成此过程 以下是我为BotTask类编写的代码:C# C中列表错误CS1503中对象的随机化实例,c#,oop,console-application,C#,Oop,Console Application,我正在用c创建一个控制台应用程序。应用程序的目的是创建机器人,创建机器人后随机分配5个任务,然后显示机器人的总时间等。我有一个BotTask和robot类 有以下问题: 我正在初始化每个BotTask,然后尝试将它们加载到一个列表中,将它们随机化并返回5个可以分配给机器人的BotTask 我在想,如果每个BotTask都有一个index属性:我可以随机化一个整数,然后使用该index属性选择哪个BotTask被random调用 当我尝试此操作时,我收到错误代码CS1503参数1:无法从“int”
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列表返回给我的Robot类。所以,在用户创建一个机器人后,将为其分配5个任务
以下是我如何在我的机器人课程中实现这一点:
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;
}
}
}
}
提前感谢您的帮助-谢谢。您不能将整数值添加到列表中 使用以下命令:
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;
}
您正在向列表中添加一个int索引。不是任务。旁注:不要在循环中创建新的随机变量。使用静态字段/属性保存单个值