简体   繁体   English

如何在C#中存储和显示用户输入

[英]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. 首先,您可能应该调用.Speak(string Message)方法以验证合成器实际上能够与您讲话。 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? 您是否知道您正在使用.SpeakAsync(string Message)启动异步任务,该任务可能与以下代码并行运行,因为尚未等待它? 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). 如果是第二种情况,而您只想显示它,则可能要运行Console.WriteLine(string Message) (假设您正在运行控制台应用程序)。

To store a string, do something like string x = Console.ReadLine(); 要存储字符串,请执行类似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(); 要发布字符串,请使用Console.WriteLine();。 or Console.Write. 或Console.Write。

example: 例:

Console.WriteLine(x); Console.WriteLine(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#新手(极其简单)如何在字段中存储用户输入并调用方法以显示字段值 - C# Newbie (extremely simple) how to store user input in a field and invoke a method to display the field value 如何将EditText用户输入存储到c#中的字符串变量中 - how to store EditText user input into a string variable in c# 将用户输入存储到 C# 中的并行数组 - Store the user input to parallel arrayin C# 如何在用户输入后将文本文件显示到控制台? 在C#中 - How to display a text file into console after user input? in C# 如何检查用户输入是否不是数字,然后在 C# 中显示消息? - How to check if user input is not a number and then display a message in C#? c#如何根据用户输入显示数组 - c# How to display an array based on user input 用户输入使用 C# 然后显示 python output 到 C# - User input using C# then display python output to C# 如何使用用户输入创建锯齿状数组,然后显示用户输入并求和C# - How to create a jagged array using user input, then display users input and sum C# 如果用户在hangman c#中猜对了,我如何显示用户的输入 - How do I display the input of the user, if the user guesses correctly in hangman c# 如何将用户输入的字符串存储在数组中以在 C# 中对其执行搜索操作? - How to store a string from user input in an array to perform search operation on it in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM