public class Word
{
private string _inputWord;
public Word()
{
Console.WriteLine("Enter a word");
_inputWord = Console.ReadLine();
}
public void sortandcount()
{
char[] test = _inputWord.ToCharArray();
char temp;
int count = 0, tcount = 0;
Array.Sort(test);
int length = test.Length;
temp = test[0];
while (length > 0)
{
for (int i = 0; i < test.Length; i++)
{
if (temp == test[i])
{
count++;
}
}
Console.WriteLine(temp + " " + count);
tcount = tcount + count;
temp = test[tcount]; //this line
length = length - count;
count = 0;
}
}
}
class Program
{
public static void Main() //this line
{
Word obj = new Word();
obj.sortandcount();
}
}
我在两行中得到了异常,我在该行中表示为注释(如//程序中的这一行),你们能否帮助我清除这个问题。该计划的主意是计算给定单词中的字符数(相同)。 如苹果 A-1 P-2 L-1 E-1c#异常索引超出范围
尝试调试,浏览代码并查看每个变量的值。这应该能够很容易地自己找出错误。 – Kjartan
http://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception.aspx。请注意,数组是零索引的,这意味着最后一个元素索引是Length-1。 –
@Kjartan当然,我会尝试和评论在这里回来,谢谢你的回复 – Ram