循环数组时 [英] While Looping an Array
问题描述
我正在尝试阅读Don Gosselin撰写的有关使用Visual C#进行ASP.NET编程的书。为了解决这个问题,我只是简单地通过遵循while循环使其工作:一个while循环用于为数组元素分配一个数字,另一个while循环用于显示该数组。阵列总数显示从1到100。应该可以,但不能。由于某些原因,Visual Studio 2013调试器分配的计数= 100,这就是失败的原因。
I'm trying to understand a book from Don Gosselin on ASP.NET Programming with Visual C#. To solve it I just simply make it to work by adhering to while loops: one while loop is to assign a number to an array element, the other while loop is to display that array. Total array count displays 1 through 100. This should have worked but didn't. Visual Studio 2013 debugger for some reason assigns count = 100, that's why it's failing.
<%
int count = 0;
int[] numbers = new int[100];
while (count <= 100)
{
numbers[count] = count;
++count;
}
while (count <= 100)
{
Response.Write(numbers[count] + "<br />");
++count;
}
%>
推荐答案
在尝试之前,您需要将计数重置为0下一个while语句。当前,第一个循环在计数达到101时结束。然后继续进行下一个计数,计数为101,因此循环自动结束。只需将count设置为0即可;在第二个while循环之前。
You need to reset the count to 0 before you attempt the next while statement. Currently, the first loop ends when it reaches a count equal to 101. WHen you proceed to the next while, the count is 101 so the loop automatically ends. Just set count = 0; before the second while loop.
这篇关于循环数组时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!