“我该如何解决?”;System.String[]”;C#中的错误?(使用数组+;For循环)

“我该如何解决?”;System.String[]”;C#中的错误?(使用数组+;For循环),c#,arrays,loops,for-loop,C#,Arrays,Loops,For Loop,好的,我正在尝试制作一个程序,它基本上使用for循环来显示一周中的几天,我的代码看起来很好,运行也很好,但是当我碰巧运行它时。。它会出现“一周中的哪一天是System.String[]”。。而我希望它显示一周中的哪一天是星期一。。。一周中的一天是星期二。。。星期三。。等等 以下是我迄今为止为此编写的代码: //Declare variables int iDays; //Declare array const int iWEEK

好的,我正在尝试制作一个程序,它基本上使用for循环来显示一周中的几天,我的代码看起来很好,运行也很好,但是当我碰巧运行它时。。它会出现“一周中的哪一天是System.String[]”。。而我希望它显示一周中的哪一天是星期一。。。一周中的一天是星期二。。。星期三。。等等

以下是我迄今为止为此编写的代码:

        //Declare variables
        int iDays;

        //Declare array
        const int iWEEK = 7;
        string[] sDays = new string[iWEEK] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        //Display the days of the week
        for (iDays = 0; iDays < iWEEK; iDays++)
        {
            Console.WriteLine("The day of the week is " + sDays);
        }

        //Prevent program from closing
        Console.WriteLine();
        Console.WriteLine("Press any key to close");
        Console.ReadKey();
//声明变量
国际日;
//声明数组
const int iWEEK=7;
string[]sDays=新字符串[iWEEK]{“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”};
//显示一周中的几天
对于(iDays=0;iDays
必须在数组内部打印值,而不是数组本身


改用
sDays[iDays]
。这将检索数组
sDays
中位置
iDays
处的值

您绝对不需要数组来显示工作日的名称。它们已经存在于系统中:

for (int i = 1; i <= 7; i++)
{
    DateTime dt = DateTime.Now;
    dt = dt.AddDays(i - (int)DateTime.Now.DayOfWeek);
    Console.WriteLine(dt.ToString("dddd", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")));
}
for(inti=1;i//声明变量
国际日

    //Declare array
    const int iWEEK = 7;
    string[] sDays = new string[iWEEK] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    //Display the days of the week
    for (iDays = 0; iDays < iWEEK; iDays++)
    {
        Console.WriteLine("The day of the week is " + sDays[iDays]);
    }

    //Prevent program from closing
    Console.WriteLine();
    Console.WriteLine("Press any key to close");
    Console.ReadKey();
//声明数组
const int iWEEK=7;
string[]sDays=新字符串[iWEEK]{“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”};
//显示一周中的几天
对于(iDays=0;iDays
非常感谢,除此之外,我几乎尝试了所有其他方法。我现在感觉相当愚蠢,我正在尝试sDays[iWEEK],这也出现了一个严重错误,谢谢!我们都在那里,没有问题。你在
sDays[iWEEK]上出现了一个错误。
因为那样会尝试访问索引7。你的数组只有索引0到6。
sDays[iWEEK]<代码>索引将超出数组的界限。< /代码>因为它将提供<代码> 7代码>代码>它比数组中存在的索引高。数组在索引<代码> 0代码>开始,所以<代码> 6 /代码>将是您可以使用的最高索引,但是它仍然是第七值!@ USER如果它解决了你的问题,请给出答案。啊,好的,是的,我现在明白了,谢谢你的帮助,谢谢:)