我承认标题看起来很混乱,我只是无法正确说出来,请放心,我会在这里深入解释我的问题。
我将首先解释一下我想要完成的事情。
我想创建一个程序,要求用户根据显示的首都城市猜测国家/地区名称。用户现在可以选择在一行中输入最多三个国家/地区名称,每个名称用逗号分隔,如果用户输入的要求超过三个,则会显示警告。然后程序将在(0-5)之间生成一个随机数。随机数将用作索引,从首都城市阵列中检索首都城市和相应的国家名称(这将是正确答案)。
我开始尝试解决它,但我不确定我最好的选择是在0-5之间生成一个链接到城市的随机数,然后将它们链接到相应的国家。
任何帮助将不胜感激如果您需要更多信息,请询问。 我将在下面发布我尝试过的代码:
string[] capitals = new string[] { "Athens", "Bangkok", "Beijing", "Berlin", "Amsterdam", "Ankara" };
string[] countrynames = new string[] { "Greece", "Thailand", "China", "Germany", "Netherlands", "Turkey" };
string random = capitals[new Random().Next(0, 5)];
WriteLine("Which country has the capital city {0}? ", random);
Write("Enter up to 3 names, comma-seperated: ");
string userinput = Console.ReadLine();
string[] temp = userinput.Split(',');
答案 0 :(得分:1)
虽然你可以通过使用局部变量同步问题的索引来做到这一点,并回答,如下所示:
var randomIndex = new Random().Next(capitals.Length);
var capital = capitals[randomIndex];
var country = countrynames[randomIndex];
WriteLine("Which country has the capital city {0}? ", capital);
// code to check user input vs country goes here
对我而言,两个独立的阵列似乎是错误的工具。由于每个问题似乎有一个答案,并且问题是唯一的,如何使用Dictionary
创建地图(或至少将问题和答案与Tuple
一起保存,或简单{{ 1}})?
class
然后,您将使用var capitalCountryMap = new Dictionary<string, string>
{
{"Athens", "Greece"}, {"Bangkok", "Thailand"}, {"X", "Y"}
};
var randomIndex = new Random().Next(capitalCountryMap.Count);
var questionAnswer = capitalCountryMap.ElementAt(randomIndex);
提问并将用户的输入与questionAnswer.Key
进行比较
将数据保持在一起的好处是,很难保持两个独立数组中的索引与项目数量变得同步。
答案 1 :(得分:0)
刚刚添加到您的代码中。当然,如果用户输入不以逗号分隔,则需要处理错误。确保妥善处理。如果您将国家/地区和大写字母都放在一个单一的数据结构中,例如dictionary
using System;
using System.Linq;
public static void Main()
{
string[] capitals = new string[] { "Athens", "Bangkok", "Beijing", "Berlin", "Amsterdam", "Ankara" };
string[] countrynames = new string[] { "Greece", "Thailand", "China", "Germany", "Netherlands", "Turkey" };
var rand = new Random().Next(0, capitals.length); // assuming it can be more than 5
string randomCapital = capitals[rand];
var correspondingRandomCountry = countrynames[rand]; // assuming your country array is in the righ order
Console.WriteLine("Which country has the capital city {0}? ", randomCapital);
Console.WriteLine("Enter up to 3 names, comma-seperated: ");
string userinput = Console.ReadLine();
string[] temp = userinput.Split(',');
var rightOrWrongString = temp.Any(t => t.ToLower().Equals(correspondingRandomCountry.ToLower())) == true ? "right answer" : "wrong answer";
Console.WriteLine("total no: of guesses made {0} - Result: {1}", temp.length, rightOrWrongString );
}
这是fiddle。
答案 2 :(得分:0)
如果您的大写数组索引等于国家/地区名称,例如
var country={"USA","Greece","Germany"};
var Capitals={"DC","Athens","Berlin"};
我的意思是,国家和那个国家的资本指数是相同的
然后你可以生成数字var Number=new Random().Next(0,5);
像这样的蚂蚁印:Console.WriteLine("Country: {0} Capital:{1}",country[Number],Capitals[Number]);
但是如果你不能保证索引将处于相同的位置,那么我建议使用class。
public class CountryWithCapital
{
publict string Country {get;set;}
public string Capital {get;set;}
public CountryWithCapital(){}//Empty default constructor
public CountryWithCapital(string Country, string Capital)
{
this.Country=Country;
this.Capital=Capital;
}
}
//Implementation.
var CountriesWithCapitals=new List<CountryWithCapital>();
CountriesWithCapitals.Add(new CountryWithCapital("here is your country","this is your capital")); //Repeat this n times you want.
var Number=new Random().Next(0,5);
Console.WriteLine("Country :{0}, Capital :{1}",CountriesWithCapitals[Number].Countrry, CountriesWithCapitals[Number].Capital);