2016-04-27 46 views
0

我在使用C#将英语转换成拉丁语时遇到了一些麻烦。除了为它制作getTranslation方法外,我还有其他一切。出于某种奇怪的原因,我无法弄清楚。如果有人可以给我一些想法,我将不胜感激。英文到猪拉丁语C#控制台应用程序

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

namespace W15M5A2_CPigLatinApp 
{ 
class W15M5A2_CPigLatinAppProgram 
{ 
    static void Main(string[] args) 
    { 
     string inputPhrase = ""; 
     string[] phraseOut; 

     DisplayInfo(); 
     inputPhrase = GetPhrase(); 
     while (inputPhrase != "") 
     { 
      Console.WriteLine(" "); 
      phraseOut = GetTranslation(inputPhrase); 
      DisplayResults(inputPhrase, phraseOut); 
      inputPhrase = GetPhrase(); 
     } 
     Console.ReadKey(); 
    } 

    public static void DisplayInfo() 
    { 
       Console.WriteLine("********************************************************" + 
      "\n*** You will be prompted to enter a string of  ***" + 
      "\n*** words. The phrase will be converted into a ***" + 
      "\n*** pseudo Pig Latin with results displayed.  ***" + 
      "\n\n*** Enter as many strings as you would like.  ***" + 
      "\n********************************************************\n\n"); 
     Console.Write("\n\n\n Press any key when you are ready to begin..."); 
     Console.ReadKey(); 
     Console.Clear(); 
    } 

    public static string GetPhrase() 
    { 
     string inputPhrase; 
     Console.WriteLine("Enter a phrase or group of words " + 
      "\nTo Exit, press the Enter key\n"); 
     inputPhrase = Console.ReadLine(); 
     return inputPhrase; 
    } 
    // GetTranslation method 
    public static string[] GetTranslation(string phraseIn) 
    { 


    } 

    public static void DisplayResults(string input, string[] output) 
    { 
     Console.Clear(); 
     Console.WriteLine("Original Phrase: " + input + "\n"); 
     Console.Write("\nNew Phrase: "); 
     foreach (string i in output) 
     { 
      Console.Write(i + " "); 
     } 
     Console.WriteLine("\n"); 
    } 

} 

}

+0

你能告诉我们

public static string ToPigLatin(string word) { string result = string.Empty; string pigSuffixVowelFirst = "yay"; string pigSuffixConstanantsFirst = "ay"; string vowels = "aeiouAEIOU"; if(vowels.Contains(word.First())) { result = word + pigSuffixVowelFirst; } else { int count = 0; string end = string.Empty; foreach(char c in word) { if (!vowels.Contains(c)) { end += c; count++; } else { break; } } result = word.Substring(count) + end + pigSuffixConstanantsFirst; } return result; } 

使用你试过吗?另请告诉我们您对拉丁猪的定义。 – Rob

回答

0
public static string[] GetTranslation(string phraseIn) 
     { 
      string vow = "aeiouyAEIOUY"; 
      var splitted = phraseIn.Split(new[] {" "}, StringSplitOptions.None); 
      List< string> ret = new List<string>(); 
      foreach (string split in splitted) 
      { 
       string vows = string.Empty; 
       string cons = string.Empty; 
       for (var i = 0; i < split.Length; i++) 
       { 
        char ch = split[i]; 
        if (vow.Contains(ch)) 
        { 
         vows += ch; 
        } 
        else 
        { 
         cons += ch; 
        } 
       } 
       ret.Add(cons + vows + "ay"); 
      } 


      return ret.ToArray(); 
     } 
0

这是一个(有点哈克)溶液。我使用Wiki中的示例对它进行了测试。在你自己的危险:)