2013-10-11 79 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