2016-11-07 100 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