c# - VS Breaking once 列表超过 2(1) 而不是循环回到 0

标签 c# visual-studio

试图循环浏览我的字符,但一旦我在最后一个字符上单击下一步,它就会中断。 visual studio 给出的响应是:ArgumentOutOfRangeException 未处理并引用我的“playerCharacter pc = player[currentPlayerIndex];”

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RPGExample
{
    public partial class Form1 : Form
    {

        private List<PlayerCharacter> player;

        int currentPlayerIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // set up an empty list
            player = new List<PlayerCharacter>();


            // add a couple of players
            player.Add(new PlayerCharacter("Attila"));

            player.Add(new PlayerCharacter("Boromir"));

            // create some weapons (could be in a list too)
            Weapon sword = new Weapon("Broadsword",40,5);

            Weapon leatherShield = new Weapon("Leather Shield",2,35);

            Weapon woodenStaff = new Weapon("Wooden Staff",30,10);

            // and some food
            Food bread = new Food("Bread", 15);

            // because Weapons and food inherit from CollectableObject
            // a player can carry either (polymorphism)

            // equip the players
            player[0].Add(sword);
            player[0].Add(leatherShield);

            player[1].Add(woodenStaff);

            player[1].Add(bread);


            //  display the player we are viewing
            DisplayPlayer(currentPlayerIndex);

        }


        // display info about a player by calling objects to string function
        public void DisplayPlayer(int playerIndex)
        {
            // we number from 1 for the user
            // try removing the brackets around playerIndex + 1 !!!
            lblPlayerInfo.Text = "Player " + (playerIndex + 1);

            PlayerCharacter pc = player[currentPlayerIndex];
            txtPlayers.Text = pc.ToString();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentPlayerIndex > 0)
            {
                currentPlayerIndex--;
                DisplayPlayer(currentPlayerIndex);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentPlayerIndex < player.Count)
            {
                currentPlayerIndex++;
                DisplayPlayer(currentPlayerIndex);
            }
        }

    }
}

最佳答案

您应该在递增/递减当前播放器索引之前仅显示播放器,或者将严格比较 (<) 替换为相等比较 (<=) :)

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        if (currentPlayerIndex >= 0)
        {
            currentPlayerIndex--;
            DisplayPlayer(currentPlayerIndex);
        }
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (currentPlayerIndex < player.Count)
        {
            currentPlayerIndex++;
            DisplayPlayer(currentPlayerIndex);
        }
    }

关于c# - VS Breaking once 列表超过 2(1) 而不是循环回到 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724194/

相关文章:

.net - 如何克隆 .NET 解决方案?

visual-studio - 如何说服 Visual Studio 在生成代码时不省略访问修饰符?

python - 有没有办法在计算机上查看所有 COM 事件?

c# - 无法将 xml 反序列化为对象

c# - ASP.NET 单选按钮已检查更改事件未触发第一个单选按钮

c# - 用 C# 编写 SQLServer 用户定义函数

visual-studio - Visual Studio 2017 声称我正在调试我的 C++/CLI 项目的发布版本

c# - WCF over net.tcp with security mode none 给出异常

c# - 我需要取消订阅动态菜单项事件吗?

visual-studio - 对 Visual Studio C++ 项目使用/Zi vs/Z7 有什么影响?