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