用户是否创建了变量? [英] Have the user create a variable?
问题描述
无论如何,当有人问我问题时,我可以让用户输入一些内容,并在用户输入之外创建一个变量吗?
IS there anyway I can have the user input something when asked a question and create a variable out of the user input?
我需要它,以便用户每次被要求命名一个城镇时都可以命名一个城镇.然后我需要显示他的姓名.我可以很容易地做到这一点,除了我会抛出一个随机字符串并要求他们这样命名一个城镇:
I need it so that the user can name a town every time he is asked to name one. and then I need to display the names he puts. I could do this easily except I have it throwing a random string and asking them to name a town so like this:
Random SString = new Random();
int Stringgen = SString.Next(1, 3);
switch (Stringgen)
{
case 1:
Console.WriteLine("You founded a town in a resourceful area! (+3 production points)");
production += 3;
Console.ReadLine();
towns++;
Console.WriteLine("Name your town!");
town1 = Console.ReadLine();
Console.Clear();
game2();
break;
上面写着town1 = Console.ReadLine();是我搞砸的地方,因为这是显示城镇名称的目的:
And where it says town1 = Console.ReadLine(); is where I am messing up because this is what I have for showing the town names:
Console.WriteLine("You have "+towns+" town/s.");
Console.WriteLine("Towns: ");
Console.WriteLine(town1);
Console.WriteLine(town2);
Console.WriteLine(town3);
Console.WriteLine(town4);
Console.WriteLine(town5);
Console.WriteLine(town6);
Console.WriteLine(town7);
Console.WriteLine(town8);
Console.WriteLine(town9);
Console.WriteLine(town10);
Devon
推荐答案
您可以使用字符串值列表在其中存储城镇名称数组.
You can use list of string values to store an array of town names in it.
我不知道您到底想做什么,但是您可以像这样存储城镇名称:
I do not know what you are trying to do exactly but you can store your town names like this:
List<string> towns = new List<String>();
switch(Stringgen)
{
情况1:
Console.WriteLine(您在资源丰富的地区建立了一个城镇!(+ 3个生产点)");
生产+ = 3;
Console.ReadLine();
Console.WriteLine(为您的城镇命名!");
town1 = Console.ReadLine();
switch (Stringgen)
{
case 1:
Console.WriteLine("You founded a town in a resourceful area! (+3 production points)");
production += 3;
Console.ReadLine();
Console.WriteLine("Name your town!");
town1 = Console.ReadLine();
towns.Add(town1);
Console.Clear();
towns.Add(town1);
Console.Clear();
并使用foreach循环显示您的城镇名称:
and to show your town names use a foreach loop:
foreach(var town in towns)
{
Console.WriteLine(town);
}
//显示城镇总数:
Console.WriteLine(towns.Count());
Console.WriteLine(towns.Count());
这篇关于用户是否创建了变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!