[英]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.