2016-11-07 6 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; 
     } 

    } 
} 

배열을 사용하여 문자열을 배열로 분할 한 다음 해당 배열을 사용하여 문자를 개별적으로 변경합니다. t이> 단어 사항 Array.length이 있어야한다 루프 당신이있어 당신의 인코더에서 작동하지 않는 몇 가지 이유 ...

답변

0

미만

+0

방금 ​​수정했는데 변경되지 않았습니다. 나는 여전히 사용자 입력과 동일한 메시지를 가지고있다 –

1

의 모두는 잘못된 시도됩니다 for (int i = 0; i < words.Length; i++)

+0

아. 그것은 그것을 고쳤다. 그것은이 코드를 위해 징후가 섞여있는 것처럼 보입니다. 고맙습니다. –

+0

@ TommyVidrio 도와 주시면 답변을 수락하십시오! – mybirthname