1

我创建了一个简单的 C# 控制台计算器,我想添加语音识别来识别人声。

示例:如果我想添加两个数字,我会通过我的声音告诉程序添加两个数字,然后它会进行计算。但我不知道该怎么做。这是代码

class Program
{
    static void Main(string[] args)
    {
        int num1;
        int num2;
        string operators;
        float ans;

        Console.Write(" first integer: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("enter a operator (+, -, /, *): ");
        operand = Console.ReadLine();
        Console.Write("enter the second integer: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        switch (operators)
        {
            case "-":
                answer = num1 - num2;
                break;

            case "+":
                answer = num1 + num2;
                break;

            case "/":
                answer = num1 / num2;
                break;

            case "*":
                answer = num1 * num2;
                break;

            default:
                answer = 0;
                break;
        }

        Console.WriteLine(num1.ToString() + " " + operators 
                                 + " " + num2.ToString() + " = " + ans.ToString());
        Console.ReadLine();
    }
}
4

1 回答 1

1
  1. 您将需要包含System.Speech参考。
  2. 当应用程序启动时,您需要发出代码来连接语音识别。请注意,该Text属性可能已经包含您期望的表达式 - 类似于1 * 2if you speak One times two

语音识别代码

SpeechRecognizer rec = new SpeechRecognizer();
rec.SpeechRecognized += rec_SpeechRecognized;
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    // parse the grammar here and perform your operations
    // write the result to the console
}
于 2012-08-29T14:10:32.073 回答