2016-11-07 179 views
0

使用手工編碼,這是假設使用戶的輸入編碼成不同的東西(字母在他們鍵入的字母之後)。每當我嘗試運行它時,回報只是用戶的句子。我很高興它可以用於解碼器,但編碼器需要對該信息進行編碼。我想知道爲什麼它不起作用。編碼器到解碼字符串

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace EncoderDecoder 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      Console.WriteLine("Please enter a sentence. No numbers, smybols, or punctuations."); 
      string sentence = Console.ReadLine(); 
      Console.WriteLine(); 


      Console.WriteLine("Your encoded message."); 
      string encodedSentence = Encode(sentence); 
      Console.WriteLine(encodedSentence); 

      Console.WriteLine("Your decoded message. Also known as your original message."); 
      string decodedSentence = Decode(sentence); 
      Console.WriteLine(decodedSentence); 

      Console.ReadLine(); 
     } 

     private static string Decode(string encodedSentence) 
     { 
      char[] wordArray; 
      string[] words = encodedSentence.Split(' '); 
      for (int i = 0; i > words.Length; i++) 
      { 
       wordArray = words[i].ToArray(); 
       if (wordArray.Length > 1) 
       { 
        char beginLetter = wordArray[0]; 
        wordArray[0] = wordArray[wordArray.Length + 1]; 
        wordArray[wordArray.Length + 1] = beginLetter; 
       } 
       for (int t = 0; t < wordArray.Length; t++) 
       { 
        wordArray[t] = (char)(wordArray[t] + 1); 
       } 
       words[i] = new string(wordArray); 
      } 
      string decoded = string.Join(" ", words); 
      return decoded; 
     } 

     private static string Encode(string sentence) 
     { 
      char[] wordArray; 
      string[] words = sentence.Split(' '); 
      for (int i = 0; i > words.Length; i++) 
      { 
       wordArray = words[i].ToArray(); 
       if (wordArray.Length > 1) 
       { 
        char beginLetter = wordArray[0]; 
        wordArray[0] = wordArray[wordArray.Length - 1]; 
        wordArray[wordArray.Length - 1] = beginLetter; 
       } 
       for(int t = 0; t > wordArray.Length; t++) 
       { 
        wordArray[t] = (char)(wordArray[t] + 1); 
       } 
       words[i] = new string(wordArray); 
      } 
      string encoded = string.Join(" ", words); 
      return encoded; 
     } 

    } 
} 

使用數組,我將字符串拆分爲數組,然後使用該數組單獨更改字母。出於某種原因,它不工作...

回答

0

在編碼器在你的循環,你已經得到了其中t>字array.length這應該是小於

+0

我只是修復了它,並沒有改變它。我仍然收到與用戶輸入相同的信息 –

1

無論是對的錯了,請嘗試:for (int i = 0; i < words.Length; i++)

+0

啊。這解決了它。似乎我得到了這個代碼混合的跡象。謝謝。 –

+0

@TommyVidrio如果幫助你,請接受他的回答! – mybirthname