Problem z dictionary i iteracją - C#

0

Witajcie!
Dlaczego poniższa funkcja niezależnie od podanej wartości zwraca pusty string?
Proszę o podanie wyjaśnień, nie gotowego kodu

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

namespace Morse_Decoder
{
	public static class MorseMethods
	{
		public static Dictionary<string, char> morseCodeLookup = new Dictionary<string, char>()
		{
			[".- "] = 'a',
			["-... "] = 'b',
			["-.-. "] = 'c',
			["-.. "] = 'd',
			[". "] = 'e',
			["..-. "] = 'f',
			["--. "] = 'g',
			[".... "] = 'h',
			[".. "] = 'i',
			[".--- "] = 'j',
			["-.- "] = 'k',
			[".-.. "] = 'l',
			["-- "] = 'm',
			["-. "] = 'n',
			["--- "] = 'o',
			[".--. "] = 'p',
			["--.- "] = 'q',
			[".-. "] = 'r',
			["... "] = 's',
			["- "] = 't',
			["..- "] = 'u',
			["...- "] = 'v',
			[".-- "] = 'w',
			["-..- "] = 'x',
			["-.-- "] = 'y',
			["--.. "] = 'z',
			[".---- "] = '1',
			["..--- "] = '2',
			["...-- "] = '3',
			["....- "] = '4',
			["..... "] = '5',
			["-.... "] = '6',
			["--... "] = '7',
			["---.. "] = '8',
			["----. "] = '9',
			["----- "] = '0',
		};
		public static string DecodeText(string TextToDecode)
		{
			string Result = "";
			string Temp = "";
			string[] MorseTextSplit = TextToDecode.Split(' ');
			for (int i = 0; i == MorseTextSplit.Length; i++)
			{
				Temp += MorseMethods.morseCodeLookup[MorseTextSplit[i]];
				Result += Temp;
			}
			return Result;
		}
	}
}
0

for (int i = 0; i == MorseTextSplit.Length; i++)
I jest zerem, więc warunek nie zostaje nigdy spełniony, chyba że podasz pustego stringa.

0

OMG, ale jesztem głupi. Wybaczcie.

0

Naprawiłem, powiedzcie mi, czy ten kod mogę napisać jakoś łądniej, lepiej, zgrabniej?

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

namespace Morse_Decoder
{
	public static class MorseMethods
	{
		public static Dictionary<string, char> morseCodeLookup = new Dictionary<string, char>()
		{
			[".-"] = 'a',
			["-..."] = 'b',
			["-.-."] = 'c',
			["-.."] = 'd',
			["."] = 'e',
			["..-."] = 'f',
			["--."] = 'g',
			["...."] = 'h',
			[".."] = 'i',
			[".---"] = 'j',
			["-.-"] = 'k',
			[".-.."] = 'l',
			["--"] = 'm',
			["-."] = 'n',
			["---"] = 'o',
			[".--."] = 'p',
			["--.-"] = 'q',
			[".-."] = 'r',
			["..."] = 's',
			["-"] = 't',
			["..-"] = 'u',
			["...-"] = 'v',
			[".--"] = 'w',
			["-..-"] = 'x',
			["-.--"] = 'y',
			["--.."] = 'z',
			[".----"] = '1',
			["..---"] = '2',
			["...--"] = '3',
			["....-"] = '4',
			["....."] = '5',
			["-...."] = '6',
			["--..."] = '7',
			["---.."] = '8',
			["----."] = '9',
			["-----"] = '0',
		};
		public static string DecodeText(string TextToDecode)
		{
			string Result = "";
			string Temp = "";
			string[] MorseTextSplit = TextToDecode.Split(' ');
			foreach (var item in MorseTextSplit)
			{
				try
				{
					Result += morseCodeLookup[item];
				}
				catch
				{
	System.Windows.Forms.MessageBox.Show("The entered morse code cannot be recognized.", "Error",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
					return "An error has occured";
				}
			}
			return Result;
		}
	}
}
0
var result = string.Empty;
try 
{
  TextToDecode.split(' ').Select(item => result += morseCodeLookup[item])
}
catch 
{
///
}
return result;

1 użytkowników online, w tym zalogowanych: 0, gości: 1