gpt4 book ai didi

c# - 控制台应用程序错误 "Index (zero based) must be greater than or equal to zero and less than the size of the argument list"

转载 作者:行者123 更新时间:2023-12-03 20:50:04 25 4
gpt4 key购买 nike

这里是静态虚空主体

        string[] dayNames = { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat" };

string m = "";

double average = 0;
double total = 0;

int[] bCalories = new int[7];
int[] lCalories = new int[7];
int[] dCalories = new int[7];

int[] dayTotal = new int[7];

for (int i = 0; i < 7; i++)
{
Console.Write("Please enter calories for {0} breakfast: ", dayNames[i]);
bCalories[i] = int.Parse(Console.ReadLine());
Console.Write("Please enter calories for {0} lunch: ", dayNames[i]);
lCalories[i] = int.Parse(Console.ReadLine());
Console.Write("Please enter calories for {0} dinner: ", dayNames[i]);
dCalories[i] = int.Parse(Console.ReadLine());

dayTotal[i] += bCalories[i];
dayTotal[i] += lCalories[i];
dayTotal[i] += dCalories[i];

total += dayTotal[i];
Console.WriteLine();
}
average = total /7;
Console.Clear();
Console.WriteLine("Day\t\tBreakfast\tLunch\tDinner\tDay Total");
for (int i = 0; i < 7; i++)
{
if (dayTotal[i] > average)
{
m = "*** Above Average";
}
else if(dayTotal[i] <= average)
m = "";

在我将字符串 m 添加到下面的 writeline 之后,我只收到错误 Index (zero based) must be greater than or equal to zero and less than the size of the argument list”。如果我没有字符串m 在语句的末尾然后程序运行正常,只有当我添加它时我得到一个错误

         Console.Write("{0}\t\t{1}\t\t{2}\t{3}\t{4}\t{5}\n", dayNames[i],
bCalories[i], lCalories[i], dCalories[i], dayTotal[i] + m);
}

Console.Write("Average daily calories: {0}", average);
Console.ReadLine();
}

最佳答案

问题出在这里:

    Console.Write("{0}\t\t{1}\t\t{2}\t{3}\t{4}\t{5}\n", dayNames[i],
bCalories[i], lCalories[i], dCalories[i], dayTotal[i] + m);

您有 6 个说明符 (0 - 5),但只传入了 5 个参数。如果您将其更改为仅包含最多 {4},它应该可以正常工作:

    Console.WriteLine("{0}\t\t{1}\t\t{2}\t{3}\t{4}", dayNames[i],
bCalories[i], lCalories[i], dCalories[i], dayTotal[i] + m);

我还建议使用 Console.WriteLine 而不是带有 \nConsole.Write。这表明您打算添加一行,这反过来又使代码更易于阅读和维护。

关于c# - 控制台应用程序错误 "Index (zero based) must be greater than or equal to zero and less than the size of the argument list",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15484823/

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