我有问题。我在c#中创建了一个语音识别程序。我希望我的程序能够检测到错误的命令。
我尝试使用try和catch但我认为我弄错了。
void Default_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
switch (case)...
{
//Commands
}
}
try
{
if (speech != e.result.Text)
Bill.Speak("You have given an invalid command. Please try again.");
}
catch{}
我该如何正确地做到这一点?
答案 0 :(得分:2)
只需在开关案例中添加默认值
switch (speech)
{
case "1":
Bill.Speak("Command 1");
break;
case "2":
Bill.Speak("Command 2");
break;
default:
Bill.Speak("You have given an invalid command. Please try again.");
break;
}
答案 1 :(得分:1)
我认为您想要的是您的交换机的默认设置。所以
switch (case)...
{
//Commands
default: // not recognized
Bill.Speak("You have given an invalid command. Please try again.");
}
答案 2 :(得分:0)
添加 默认: Bill.Speak(“我不理解命令”);
到您的案例陈述