如何在c#

时间:2017-10-01 13:57:11

标签: c#

我正在寻找用户输入信息,该信息应存储在字符串中,以便程序能够检索并说出来。

 *

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Synthesis;
using Microsoft.Speech.Synthesis;
using System.Media;


namespace Game2
{
    class Program
    {
        static void Main(string[] args)

        {
           /* SoundPlayer player = new SoundPlayer();
            player.SoundLocation = AppDomain.CurrentDomain.BaseDirectory + "\\mozart.wav";
            player.Play();*/
            System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();
            synth.SetOutputToDefaultAudioDevice();
            foreach (var v in synth.GetInstalledVoices().Select(v=>v.VoiceInfo))
            {
                Console.WriteLine("Name:{0}, Gender:{1}, Age:{2}",
                  v.Description, v.Gender, v.Age);
            }
            synth.SelectVoiceByHints(System.Speech.Synthesis.VoiceGender.Female, System.Speech.Synthesis.VoiceAge.Child);
            string name = "";
            string age = "";
            bool gender = true;
            string attire = "";
            string title = "";
            bool voice = true;
            synth.Speak("Would you like to receive your commands by voice as well?");
            Console.WriteLine("Would you like to receive your commands by voice as well?");
            Console.ReadKey();
            /*if (voice = true) 
                    {
                synth.SpeakAsync.

            }
            else
            {
            }
                 */
            synth.Speak("Please type your name?");
            Console.WriteLine();
            name = Console.ReadLine();
            synth.Speak("Please type your age?");
            age = Console.ReadLine();
            /* synth.SpeakAsync("Please type if you are male or female?");
             Console.WriteLine();
             if 
                 {
                 gender = "male";
                 string male = "Mr";
             }
             else
             {
                 gender = "female";
                 string female = "Ms.";
             }

            synth.SpeakAsync("I have checked your identification and you are {0},{1},{2}", title, name, age);  
            */             
            synth.SpeakAsync("What would yu like to wear, when coming to my playground?" +
                " Remember, what you wear will determine where you can go and do");
            synth.SpeakAsync("Please type in smart or casual?");
            Console.WriteLine();
            attire = Console.ReadLine();
            synth.SelectVoiceByHints(System.Speech.Synthesis.VoiceGender.Female, System.Speech.Synthesis.VoiceAge.Teen);
            synth.SpeakAsync("Welcome aboard Gamania Playground");
            Console.ReadLine();

     synth.SpeakAsync("Where would you like to go?" +
                "you can go straight to the Reception room");

            }
        }
    }
  • 我已经管理程序接受并将用户输入存储为字符串,虽然没有显示错误,但它不会运行此部分。

我是新手,通过研究,试验和错误学习,所以请耐心等待我

2 个答案:

答案 0 :(得分:0)

你的问题标题实际上有点误导。你想把它播放到扬声器吗?或者你只想显示它?

如果是第一种情况:

首先,您应该调用.Speak(string Message)方法来验证合成器实际上是否能够与您交谈。您也可以调试它以确保调用该方法。

如果是这种情况,那么您可能需要查看您的程序流程。您是否知道您正在启动一个.SpeakAsync(string Message)的异步任务,该任务可能与以下代码并行运行,因为它不等待?是否可以在该行甚至能够执行之前终止应用程序?

如果是第二种情况并且您只想显示它,则可能需要运行Console.WriteLine(string Message)(假设您正在运行控制台应用程序)。

答案 1 :(得分:0)

要存储字符串,请执行string x = Console.ReadLine();之类的操作 这将使它们放入控制台的任何内容都保存为字符串的值。

要发布字符串,请使用Console.WriteLine();或Console.Write。

示例:

Console.WriteLine(X);