0

所以我正在用 C# 编写一个语音识别程序,并且我编译了几行代码,speaks back the current battery level当我说"battery Level".唯一的问题是,它不起作用。

调试阶段它构建得很好,当我说“电池电量”时没有错误或警告,我没有得到任何回应。

if (e.Result.Text == "battery level")
        {
            System.Management.ManagementClass wmi = new System.Management.ManagementClass("Win32_Battery");
            var allBatteries = wmi.GetInstances();
            String estimatedChargeRemaining = String.Empty;

            foreach (var battery in allBatteries)
            {
                estimatedChargeRemaining = Convert.ToString(battery["EstimatedChargeRemaining"]);
            }

            JARVIS.Speak("Estimated Charge Remaining: " + estimatedChargeRemaining + "%");
            return;
        }

有没有人注意到代码中有任何明显的错误可能会阻止它工作?

谢谢。

4

1 回答 1

0

您的代码中有几个错误可能会也可能不会影响您的应用程序

if (e.Result.Text.ToLower() == "battery level")   //First Change
{
    System.Management.ManagementClass wmi = new System.Management.ManagementClass("Win32_Battery");
    var allBatteries = wmi.GetInstances();
    String estimatedChargeRemaining = String.Empty;

    foreach (var battery in allBatteries)
    {
       estimatedChargeRemaining = Convert.ToString(battery["EstimatedChargeRemaining"]);
       JARVIS.Speak("Estimated Charge Remaining: " + estimatedChargeRemaining + "%");  //second change as you may have more than one batteries.
    }       
    return;
}
于 2013-08-08T11:15:16.507 回答