为什么YYYYYY?
以下是我的代码的大约一半,因为我已经删除了大部分代码以及我在尝试解决这个问题时失去的每一根头发。无论我删除或更改什么,我都会不断收到 StackOverflowException。
这非常奇怪,因为这个完全相同的代码更早地工作了。
请给我任何建议,因为我一无所知...
我已经检查过了,但我认为这不是发生的事情:
- 无限递归循环
- 程序只是用完了堆栈空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace ConsoleApplication10
{
class Program
{
public int currentDialogueID = 0;
List<NPC> NPCList = new List<NPC>() {
new NPC(0, "Man", 1, true),
new NPC(1, "Woman", 1, true),
new NPC(2, "Troll", 3, true)
};
static void Main(string[] args)
{
Program d = new Program();
d.Init();
Console.ReadKey(false);
}
void Init()
{
getNPCByName("Man").NPCDialogue();
}
public NPC getNPCByName(string npcName)
{
IEnumerable<NPC> myNPCsName = from nn in NPCList
where nn._name.ToLower() == npcName.ToLower()
orderby nn._name ascending
select nn;
foreach(NPC nn2 in myNPCsName)
{
return nn2;
}
return null;
}
public NPC getNPCByID(int npcID)
{
IEnumerable<NPC> NPCsByID = from ni in NPCList
where ni._npcID == npcID
orderby ni._npcID ascending
select ni;
foreach(NPC ni2 in NPCsByID)
{
return ni2;
}
return null;
}
public string getNPCNameByID(int npcid)
{
return getNPCByID(npcid)._name;
}
}
class NPC : Program
{
public string _name = "null";
public int _level;
public int _npcID;
public int _maxDamage;
public bool _canFight = false;
public NPC(int npcID = 0, string name = "null", int level = 0, bool canSpeak = false, bool canFight = false, int maxDamage = 0)
{
_level = level;
_name = name;
_npcID = npcID;
_canFight = canFight;
_maxDamage = maxDamage;
}
public void NPCDialogue()
{
currentDialogueID = _npcID;
switch(_npcID)
{
case 0:
NPCSpeak("Man test... ... ...");
break;
case 1:
NPCSpeak("Woman test");
break;
case 2:
NPCSpeak("I'm Elad the Troll, Ramzes your ear is that of an elf");
break;
default:
return;
}
}
public void NPCSpeak(string text, int npcID = 99999)
{
if(npcID == 99999)
npcID = currentDialogueID;
if(npcID != 99999)
type(getNPCNameByID(npcID) + ": " + text);
}
public void type(string x)
{
Random rnd = new Random();
char[] xx = x.ToCharArray();
for(int i = 0; i < xx.Length; i++)
{
Console.Write(xx[i]);
System.Threading.Thread.Sleep(rnd.Next(10, 120));
if(xx[i] == ':' || (xx[i] == '.' && xx[i - 1] != '.' && xx[i + 1] != '.') || xx[i] == '!' || xx[i] == '\n' || xx[i] == '?')
{
System.Threading.Thread.Sleep(rnd.Next(400, 1500));
}
}
}
}
class Item : Program
{
public string _name = "null";
public string _description = "How did you get this?";
public bool _isWeapon = false;
public int _maxDamage;
public int _itemID = 0;
public Item(int itemID = 0, string name = "null", string description = "null", bool isWeapon = false, int maxDamage = 0)
{
_itemID = itemID;
_name = name;
_description = description;
_isWeapon = isWeapon;
_maxDamage = maxDamage;
}
}
}