2013-10-11 60 views
-6
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#异常索引超出范围

+3

尝试调试,浏览代码并查看每个变量的值。这应该能够很容易地自己找出错误。 – Kjartan

+0

http://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception.aspx。请注意,数组是零索引的,这意味着最后一个元素索引是Length-1。 –

+0

@Kjartan当然,我会尝试和评论在这里回来,谢谢你的回复 – Ram

回答

0

如果要输出单词中的字母数,请尝试以下代码:

var testString = "APPLE"; 

testString.ToCharArray() 
.OrderBy(i => i).ToLookup(i => i) 
.Select(i => new { letter = i.Key, count = i.Count() }).ToList() 
.ForEach(i => Console.WriteLine("letter {0}, count {1}", i.letter, i.count)); 

这是一个更清洁,更容易出错。

2

当你数了所有的信件,然后tcount == test.length这意味着test[tcount]将索引一个元素到0为止。

给定任何数组arr,那么arr[arr.length]将始终超出范围,因为arr是零索引。温度之前=试验[TCOUNT]你需要确保tcount < test.length但是你也有一个错误在你的逻辑

尝试用字obo,将打印o 2 o 2

一个简单实现一个字计数构成特征的(如果订单并不一定是因为它们出现在单词)将

var result = test.Aggregate(new Dictionary<char,int>(), (state,c)=>{ 
       if(!state.ContainsKey(c)) { state.Add(c,0); } 
       state[c] += 1; 
       return state; 
      }); 

foreach(var pair in result) { Console.WriteLine(pair.Key + " " + key.Value); } 

编辑,如果你需要它们,因为它们出现在单词然后更改的foreach到这是在同一顺序进行排序

foreach(var pair in result.OrderBy(p=>test.IndexOf(p.Key))) { 
    Console.WriteLine(pair.Key + " " + key.Value); 
} 
+0

如果什么顺序应该表现为与输入相同 – Ram

0

的代码包含一个错误

int length = test.Length; // This is not zero based 

和计数从零开始,你的循环会做一个额外的迭代造成

temp = test[tcount] 

失败,因为TCOUNT现在变得比长度大的测试1个字符。

的最好的事情就是

int length = test.Length -1; 

请让我知道如果这有助于:)有一个愉快的一天

+0

如果条件我只是说这一点,所以多一个迭代问题就解决了,感谢帮助我 如果(TCOUNT!= test.length) 温度= test [tcount]; – Ram

+0

大^^我很高兴你拿出一个解决方案 –

0

多一点点 “纲领性” 的版本:

public class Word 
{ 
    private string _inputWord; 
    public Word() 
    { 
     Console.WriteLine("Enter a word"); 
     _inputWord = Console.ReadLine(); 
    } 

    public void SortAndCount() 
    { 
     // sort 
     char[] array = _inputWord.ToCharArray(); 
     Array.Sort(array); 
     // for all characters 
     for(int i = 0; i < array.Length; i++) 
     { 
      // duplicate check 
      if(i > 0 && array[i] == array[i - 1]) 
       continue; 
      // count 
      int count = 0; 
      for(int j = 0; j < array.Length; j++) 
       if(array[i] == array[j]) 
        count++; 
      Console.WriteLine(array[i] + " " + count); 
     } 
    } 
} 

class Program 
{ 
    public static void Main() 
    { 
     Word obj = new Word(); 
     obj.SortAndCount(); 
    } 
} 
+0

你的代码比我的可读性更强。但是使用两个for循环比需要更多的迭代,所以我去了。 – Ram

+0

@Ram,添加重复检查。 – Sinatr