c#这个switch超出索引怎么解决?

在C#程序中遇到switch超出索引的情况,通常是因为尝试访问数组或集合的元素时,使用的索引超出了其实际长度。在给出的代码示例中,可能存在未展示的部分导致错误。解决这个问题需要检查switch语句中的变量是否正确引用了数组或集合的索引,并确保索引值在0到长度-1之间。确保在访问PlayerPos或PlayerNames等数组时,避免越界操作。
摘要由CSDN通过智能技术生成

namespace 飞行棋
{
class Program
{
static int[] Maps = new int[100];
static int[] PlayerPos = new int[2];
static string[] PlayerNames = new string[2];
static bool Flags;
static void Main(string[] args)
{
GameShow();//标题
InitailMap();//特殊事件区域
DrawMap();//画图
#region 接受玩家AB的姓名
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(“请输入玩家A的姓名”);
PlayerNames[0] = Console.ReadLine();

        while (PlayerNames[0] == "")
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("玩家A的姓名不能为空,请重新输入");
            PlayerNames[0] = Console.ReadLine();
        }
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("请输入玩家B的姓名");
        PlayerNames[1] = Console.ReadLine();
        while (PlayerNames[1] == PlayerNames[0] || PlayerNames[1] == "")
        {
            if (PlayerNames[1] == "")
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("玩家B的姓名不能为空");
                PlayerNames[1] = Console.ReadLine();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("玩家B的姓名不能与玩家A的姓名相同,请重新输入");
                PlayerNames[1] = Console.ReadLine();
            }
        } 
        #endregion
        Console.Clear();//清屏
        GameShow();//标题
        Console.WriteLine("玩家{0}用A表示",PlayerNames[0]);
        Console.WriteLine("玩家{0}用B表示", PlayerNames[1]);
        InitailMap();//特殊事件区域
        DrawMap();//画图
        while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
        {
            if (Flags == false)
                PlayerNumbe(0);
            else
                Flags = false;
            if(PlayerPos[0]>=99)
            {
                Console.WriteLine("玩家:{0}胜利了", PlayerNames[0]);
                break;
            }
            if (Flags == false)
                P
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值