Error 1 Cannot apply indexing with [] to an expression of type 'int'

一曲冷凌霜 提交于 2019-12-19 03:59:19

问题


I am using Microsoft Visual Studio 2010 and C#. This is a function that is being called form elsewhere in my console program, but I can't seem to get input into the array 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;
    }
}

回答1:


You need to declare ipoints as an array of ints, not just an int.

Change int ipoints to int[] ipoints.




回答2:


You're trying to use an int like an int[] array.

Here:

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

You're doing it here:

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

Either make it an array, or rethink what you're trying to do.



来源:https://stackoverflow.com/questions/21081690/error-1-cannot-apply-indexing-with-to-an-expression-of-type-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!