简体   繁体   中英

How to store and display user input in c#

I am looking for the user to type in the information, which should be stored in a string and for the programme to be able to retrieve it and speak it.

 *

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");

            }
        }
    }
  • I have managed the programme to accept and store the user input as Strings and, whilst there is no errors shown it doesn't run this part.

I am a novice and learning by research, trial and error so please bear with me

Your question title is actually a bit misleading. Do you want to play it to the speakers? Or do you just want to display it?

If the first is the case:

First you should probably call the .Speak(string Message) method to validate that the syntheziser is actually able to speak with you. You can also debug it to make sure the method gets called.

If this is the case, then you might want to look at your program flow. Are you aware that you are starting an asynchronous task with .SpeakAsync(string Message) that might run in parallel to the following code, because it is not awaited? Could it be that you are terminating the application before that line is even able to execute?

If the second is the case and you just want to display it, you might want to run Console.WriteLine(string Message) instead (assuming you are running a console application).

To store a string, do something like string x = Console.ReadLine(); This will make it so that whatever they put into the console is saved as the string's value.

To post a string, use Console.WriteLine(); or Console.Write.

example:

Console.WriteLine(x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM