【发布时间】:2013-10-19 04:11:18
【问题描述】:
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();
}
}
我在两行出现异常,我在该行上表示为注释(如//程序中的这一行),你能帮我清除一下吗?该程序的 M 想法是计算给定单词中的字符数(相同)。 例如苹果 一个-1 p-2 l-1 e-1
【问题讨论】:
-
尝试调试、单步调试代码并查看每个变量的值。这应该可以很容易地自己找出错误。
-
msdn.microsoft.com/en-us/library/…。请注意,数组是零索引的,这意味着最后一个元素索引是 Length-1。
-
@Kjartan 当然,我会尝试并在这里评论,感谢您的回复
-
感谢您的帮助,我发现了我的错误并纠正了它@Kjartan,
-
@Ram 很高兴为您提供帮助! :)