【问题标题】:Error 1 Cannot apply indexing with [] to an expression of type 'int'错误 1 ​​无法将带有 [] 的索引应用于“int”类型的表达式
【发布时间】:2026-02-15 16:45:01
【问题描述】:

我正在使用 Microsoft Visual Studio 2010 和 C#。这是一个在我的控制台程序的其他地方被调用的函数,但我似乎无法将输入输入到数组ipoints

static void GetPoints(int ipoints, string srestaurant)
{
    int index = 0;
    int iinput;
    for (index = 0; index < 5; index++)
    {
        Console.Write("please enter how many points " + srestaurant[index] + " got : ");
        iinput = Convert.ToInt32(Console.ReadLine());

        while (iinput < 0 && iinput > 20)
        {
            Console.Write("please enter how many points " + srestaurant[index] + " got : ");
            iinput = Convert.ToInt32(Console.ReadLine());
        }
        ipoints[index] = iinput;
    }
}

【问题讨论】:

    标签: c# arrays visual-studio-2010 console-application


    【解决方案1】:

    您需要将 ipoints 声明为一个整数数组,而不仅仅是一个整数。

    int ipoints 更改为int[] ipoints

    【讨论】:

    • 感谢帮了大忙,感谢您的快速回复
    【解决方案2】:

    您正在尝试使用 int 之类的 int[] 数组。

    这里:

    static void GetPoints(int ipoints, string srestaurant)
    //                    ^^^^^^ not an array
    

    你在这里做:

    ipoints[index] = iinput;
    //     ^^^^^^^ its not an array
    

    要么把它变成一个数组,要么重新考虑你想要做什么。

    【讨论】:

      最近更新 更多