c# - 在 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;
}
在没有故意在这里添加第二个问题的情况下,我想将此 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;
}
}
}
}
提前感谢任何帮助-谢谢。
解决方案
您不能将整数值添加到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;
}
推荐阅读
- python - 使用 C API 和 `object` 的 `tp_basicsize` 从任意 Python 类派生?
- asynchronous - 为什么在使用 .await 或在 `join!` 之类的宏中会出现“特征绑定的 Future 不满足”?
- python - Python协程在产量上给出未知的无
- php - 我如何在 Laravel 中正确处理错误?
- python - 将 Google Colab 笔记本转换为 PDF/HTML?
- python - 图无法按拓扑顺序排序
- r - 从长格式数据框中计算股票收益
- java - 过滤列表后点击 ListView Item
- jquery - 即使填写了必填字段,Jquery Validate 插件也会引发错误
- laravel - Laravel、Vuejs、axios 获取登录用户的帖子