【发布时间】:2022-09-23 14:09:30
【问题描述】:
我需要帮助,我正在创建一个允许输入日期的机器人,我需要验证用户是否输入了相应的日期,否则它会返回并要求他输入日期。
当我这样做时,它会在模拟器中执行机器人时引发无限循环
public PruebaOpciones()
{
var waterfallStep = new WaterfallStep[]
{
SetPeriodo,
Confirmation,
FinalProcess
};
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallStep));
AddDialog(new TextPrompt(nameof(TextPrompt)));
}
private async Task<DialogTurnResult> SetPeriodo(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
while (true)
{
string periodo = \"Ingresa el mes que quieres consultar por favor.\";
await stepContext.PromptAsync(
nameof(TextPrompt),
new PromptOptions
{
Prompt = MessageFactory.Text(periodo),
},
cancellationToken
);
periodo = periodo.ToLower();
int periodoLength = periodo.Length;
if (periodoLength == 1)
{
periodo = \"0\" + periodo;
break;
}
string[] periodoList = { \"enero\", \"febrero\", \"marzo\", \"abril\",\"mayo\",\"junio\",\"julio\",\"agosto\",\"septiembre\",\"octubre\",
\"noviembre\",\"diciembre\",\"01\", \"02\", \"03\", \"04\", \"05\",\"06\",\"07\",\"08\",\"09\",\"10\",\"11\",\"12\"};
List<string> periodoRange = new List<string>(periodoList);
if (periodoRange.Contains(periodo))
{
break;
}
else
{
return await SetPeriodo(stepContext, cancellationToken);
}
}
return await stepContext.ContinueDialogAsync(cancellationToken: cancellationToken);
}
[1]: https://i.stack.imgur.com/CIb4s.png
我怎样才能解决这个问题,而不是给我一个无限循环,只问我一次,如果我输入错误,回来问我?
-
永远不要使用 while true ......在循环中放入一些东西以使其在 while 上停止
-
您是否在调试器中设置了程序并输入了您期望应该跳出循环的值。当您逐步执行您的程序时,会发生什么?你们中的一个测试是否按照您期望的方式运行。
while (true) { }没有任何问题,但您可以确保不会卡在那里
标签: c# bots botframework