小编use*_*553的帖子

一个类型,但像变量一样使用?

    struct Player
    {
        public string Name;
        public int X;
        public int Y;
    }
    static Player[] players = new Player[amountofPlayers];

        for (int i = 1; i < amountofPlayers; i = i + 1)
        {
            int displayNumber = i + 1;
            Console.Write("Please enter the name of player " + displayNumber + ": ");
            Player[i].Name = Console.ReadLine(); // The error is here

        }
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗,因为我看不出我哪里错了...

c# variables types

3
推荐指数
1
解决办法
664
查看次数

C#中的indexoutofrangeexception

任何人都可以帮我解决问题吗?

struct Player
{
    public string Name;
    public int X;
    public int Y;
}
static Player[] players = new Player[amountofPlayers];

static void ResetGame() {
    Console.WriteLine("Welcome to the game!");
    Console.Write("How many player will be taking part today: ");
    string playerNo = Console.ReadLine();
    amountofPlayers = int.Parse(playerNo);
    if (amountofPlayers <= 4)
    {
        for (int i = 0; i < amountofPlayers; i = i + 1)
        {
            int displayNumber = i + 1;
            Console.Write("Please enter the name of player " + displayNumber + ": …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
129
查看次数

索引数组超出范围

if (testModetrue)
{
    try
    {
        Console.Write("What number do you want the roll to be set to? (1-6)");
        string diceString = Console.ReadLine();
        int diceCheck = int.Parse(diceString);
        if ((diceCheck >= minDiceValue) || (diceCheck <= maxDiceValue))
        {
            diceNo = int.Parse(diceString);
        }
        else if ((diceCheck <= minDiceValue) || (diceCheck >= maxDiceValue))
        {
            Console.WriteLine("Please enter a number between 1-6.");
            break;
        }
    }
    catch (Exception)
    {
        Console.WriteLine("An error has occured.");
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码检查给出的答案是否超过6或低于1,但无论何时我运行它,无论如何它会抛出数组错误,有人帮忙吗?

c#

1
推荐指数
1
解决办法
128
查看次数

标签 统计

c# ×3

types ×1

variables ×1