似乎找不到我要去的地方,应该早点测试一下,很确定这里有一个主要的逻辑错误 。我正在尝试根据用户输入从文本文件中列出国家/地区,但出现此错误,我尝试删除了大部分代码,并尝试键入其他测试代码,但仍然遇到重大错误,不知道我在做什么错在这里,如果有人可以告诉我这里有什么问题,将非常感谢。基本上,如果用户输入的是大写字母,该程序也应该在以后列出所有国家/地区,但我可以稍后再访问。
class CountryProgram
{
static void Main(string[] args)
{
CountryList printSearch = new CountryList();
printSearch.findAll();
}
public class CountryList
{
//declare all variable lists for each search
public StreamReader filePath = new StreamReader("countries.txt");
public static List<string> countryItems = new List<string>();
public static List<string> capitalItems = new List<string>();
public static List<string> populationItems = new List<string>();
public static List<string> allItems = new List<string>();
Country memberName = new Country();
//string retriever read all inputs
public string getString()
{
//<<<<<==============Country Search===================>>>>>
Console.WriteLine("Enter the first letter of the countries you wish to search: "); //prompts user to enter the country
string upperCaseInput = Console.ReadLine(); //gets input
string line = ""; //creates a empty string
bool finished = false;
List<string> upperList = new List<string>(); //creates a list for user input chars
foreach (char item in upperCaseInput) //adds all chars into a list named chars
{
if ( Char.IsUpper(item)) //checks if upper case and add to list if true
{
upperList.Add(item.ToString());
memberName.startsWith = true; //Assigns startsWith to true the moment a char entered is uppercase
}
}
//Begin listing countries once startsWith == true
while (((line = filePath.ReadLine()) != null) && (finished = false))
{
if (memberName.startsWith == true) // if upper case in inputted loop through the first column listing countries
{
countryItems.Add("test output"); //supposed to write results based on inputted upper case char item above
}
else //if not upper case show error message and end program
{
finished = true;
}
if (finished == true)
{
Console.WriteLine("Please enter one character in capital letters");
Console.ReadLine();
}
}
//<<<<<==============Capital Search===================>>>>>//
return countryItems.ToString();
}
//executing write to control panel depending on userinput
public void findAll()
{
Console.WriteLine(memberName.getString());
Console.ReadLine();
}
}
class Country : CountryList
{
//list variable properties
public string name { get; set; }
public string capital { get; set; }
public int population { get; set; }
public bool startsWith { get; set; }
public bool capitalHas { get; set; }
public bool lesserPopulation { get; set; }
//list counstructors
public Country()
{
}
public Country(string n, string c, int p)
{
name = n;
capital = c;
population = p;
}
}
答案 0 :(得分:1)
Country
源自CountryList
。当您尝试创建CountryList
的实例时,它也会尝试创建Country
的项(对于memberName
字段)。但是,Country
还包含一个memberName
字段,也需要创建它。对于Country
的每个实例,创建一个Country
的另一个实例,直到耗尽堆栈空间-StackOverflowException
。
您实际上没有理由要从Country
导出CountryList
。如果CountryList
应该是国家/地区列表,则只需在列表中包含项目即可。