While循环过早结束

本文关键字:结束 循环 While | 更新日期: 2023-09-27 18:20:57

我正试图创建一个程序,在该程序中,CommonCharacters Student和HonorsStudent(CommonCharacter的子类)通过试图让彼此获得更低的测试分数来"战斗"。在我的主课(见下文)中,我试图模拟角色之间的"战斗",但我很难让角色继续"战斗"直到只剩下一个。我在while语句的条件中使用了的Count属性,这样它就会循环,直到计数达到1。因为循环在运行3次后结束,所以我从来没有机会从中删除对象。(荣誉当学生的afterScore为<=50时将删除,当学生的posterScore是<=25时将删除)。

我的while循环有什么问题吗?

class Program
{
    static void Main(string[] args)
    {
        List<CommonCharacter> characters = new List<CommonCharacter>();
        characters.Add(new Student("Sarah", "female", 1));
        characters.Add(new Student("Kevin", "male", 2));
        characters.Add(new HonorsStudent("Matthew", "male", 3));
        characters.Add(new HonorsStudent("Gwen", "female", 4));
        // This will be used to choose a character to "battle" at random from the List of characters
        Random random = new Random();
        while(characters.Count > 1)
        {
            foreach(CommonCharacter CommonCharacter in characters)
            {
                int randomStudent = random.Next(characters.Count);
                // This makes sure there is no instance where a student "attacks" themself
                if (CommonCharacter.Name == characters[randomStudent].Name)
                {
                    random.Next(characters.Count);
                }
                else
                {
                    int points = CommonCharacter.TakeTest();
                    if (points > 0)
                    {
                        int afterScore = 100 - points;
                        Console.WriteLine(CommonCharacter.Name + " reports " + characters[randomStudent].Name + " for cheating and loses them " + points + " points.");
                        Console.WriteLine(characters[randomStudent].Name + " now has a test score of " + afterScore);
                        Console.WriteLine();
                    }
                    else if (points == 0)
                    {
                        Console.WriteLine(CommonCharacter.Name + " reports " + characters[randomStudent].Name + " for cheating but the claim is dismissed.");
                        Console.WriteLine();
                    }
                }
            }
            // This loop checks if any Students have fled
            for (int i = 0; i < 2; i++)
            {
                if (characters[i].HasLeftClassroom() == true && characters[i].Position == "student")
                {
                    characters.RemoveAt(i);
                    Console.WriteLine(characters[i].Name + " has received below a 25 and has left the classroom.");
                }
            }
            // This loop checks if any HonorsStudents have fled
            for (int i = 2; i < 4; i++)
            {
                if (characters[i].HasLeftClassroom() == true && characters[i].Position == "honors student")
                {
                    characters.RemoveAt(i);
                    Console.WriteLine(characters[i].Name + " has received below a 50 and has left the classroom.");
                }
            }
            break;
        }

While循环过早结束

while循环以break语句结束。中断会立即终止循环。如果您不希望循环终止,请不要编写break语句。

养成使用调试器逐行遍历代码的习惯,并仔细检查每一行。养成这种习惯会让你自己发现这样的缺陷。

我还注意到,有一个语句仅由对Next的调用组成,但它会丢弃结果。这几乎可以肯定是错误的;该方法只对结果有用!

这里可能有很多虫子。再次进入调试器,逐行执行,并验证每一行是否都执行。由于至少有一行没有按您的意愿执行,这就是错误所在。