gpt4 book ai didi

c# - 如何修复 C# 中的 "System.String[]"错误? (使用数组+For循环)

转载 作者:行者123 更新时间:2023-12-02 21:11:37 27 4
gpt4 key购买 nike

好吧,我正在尝试制作一个基本上使用 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();

最佳答案

您必须打印数组内的值,而不是数组本身。

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

关于c# - 如何修复 C# 中的 "System.String[]"错误? (使用数组+For循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com