【发布时间】:2018-04-06 00:12:09
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public const int N = 10;
static void Main(string[] args)
{
char[] word = Console.ReadLine().ToCharArray();
int i = 0, j = 0;
Console.WriteLine(word);
while ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))
{
j++;
i++;
}
Console.WriteLine(+j);
Console.ReadLine();
}
}
}
每次我尝试调试时,调试器都会告诉我“IndexOutOfRangeException was unhandled”,我不知道原因。
【问题讨论】:
-
您永远不会检查
word数组的长度。如果所有字符都是字母,则循环将继续,直到i超出数组末尾,然后您将得到异常。 -
提示:在提问之前,请确保搜索错误消息/异常名称(如 bing.com/search?q=c%23+IndexOutOfRangeException),如果您仍然决定提问,请遵循 minimal reproducible example 指南以提供包含所有必要信息的最少代码内联(即在本例中为
"A".ToCharArray()[1]")
标签: c# arrays string while-loop conditional-statements