c# - 由于遇到 StackOverFlowException 错误而终止进程,我哪里出错了?
问题描述
似乎找不到我哪里出错了,应该早点测试一下,很确定这里有一个重大的逻辑错误。我正在尝试根据用户输入从文本文件中列出国家/地区,但出现此错误,我尝试删除大部分代码并尝试输入其他测试代码,但仍然出现重大错误,不知道我做错了什么如果有人可以告诉我这里的问题是什么,那将非常感谢。基本上,如果用户输入是大写字母,该程序还应该稍后列出所有国家,但我可以稍后再谈。
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;
}
}
解决方案
Country
源自CountryList
。当您尝试创建 的实例时CountryList
,它还会尝试创建Country
(用于memberName
字段)的项目。但是,Country
也包含一个memberName
字段,也需要创建它。对于 的每个实例Country
,您都会创建另一个实例,Country
直到您用完堆栈空间 - StackOverflowException
。
你真的没有理由想要Country
从CountryList
. 如果CountryList
应该是国家列表,只需让列表包含项目即可。
推荐阅读
- javascript - RESCO OfflineHtml:无法调试 Javascript 代码
- flutter - 在颤动中使用 AlertDialog 从数据库中显示选定的数据
- visual-studio - 将 Visual Studio 连接到远程 Docker 卷
- python - 如何定义根据 CXYPY 中的变量改变值的参数
- neo4j - Cypher 查询中的 UNWIND 语句
- css - 背景大小不适用于普通类是否需要任何解决方法
- javascript - 如何拥有多个带有上一个、下一个和添加图标的 HTML5 画布
- mysql - Ansible 访问因“root@localhost”错误而被拒绝
- c++ - 旋转矩阵并删除字符并找到最大的正方形
- docker - 如何更改/升级 Docker 映像的一部分?图像内的模块,而不是整个图像