2015-11-04 59 views
0

我正在处理家庭作业问题,只需在此区域提供一些帮助。我不是要求你为我做作业,只需要一些帮助,因为我在网上找不到任何其他信息。如何允许用户输入多个字符串到数组中,直到输入字母“q”

我已经创建了一个将字符串数组作为其参数的重载方法。我相信我正确地创建了这个方法,让我可以让用户输入多个存储在数组中的字符串。

我现在在静态void main中工作,并且在想要实现以允许用户输入多个字符串的代码时遇到困难,直到用户在其自己的行上输入'q',其中它会向用户显示他们刚刚输入了多少单词。

请参阅下面的代码。你将能够看到重载方法和我被卡在静态void main中的部分,该部分用星号括起来。

再次,我不是要求你做我的老板,我只需要这一部分的一些帮助。另外,这里是我需要该程序执行的一个示例。先进的谢谢您的帮助。

输入几个句子,完成句子输入时,在最后一行使用q本身。

你好,你好吗?

我很好,你呢?

很好,很好。

q

该内容共有12个单词。

输入几个句子,当完成输入句子时,在最后一行使用q本身。所有环路的

q

static void Main(string[] args) 
    { 
     Console.WriteLine("Main Menu"); 
     Console.WriteLine("Choose from the following:"); 
     Console.WriteLine("1. Word Count" + "\n" + "2. Vowel Counting" + "\n" + "3. Exit"); 
     Console.Write("Enter your selection: "); 
     int s1 = Convert.ToInt32(Console.ReadLine()); 

     if(s1 == 1) 
     { 
     Break:; 
      Console.WriteLine("Word Counter Menu"); 
      Console.WriteLine("Choose from the following"); 
      Console.WriteLine("1. Count the word in one sentence" + "\n" + 
           "2. Count the word in a paragraph" + "\n" + 
           "3. Parent Menu"); 
      Console.Write("Enter your selection "); 
      int s2 = Convert.ToInt32(Console.ReadLine()); 

      if(s2 == 1) 
      { 
       string sent1; 

       while (true) 
       { 
        Console.Write("Enter a sentence (q to quit, d for default): "); 
        sent1 = Console.ReadLine(); 
        Console.WriteLine("There are " + Def1(sent1) + " words in the sentence: " + sent1); 

        if(sent1 == "d") 
        { 
         sent1 = "Hello"; 
         Console.WriteLine("There are " + Def1(sent1) + " words in the sentence: " + sent1); 
        } 
        if(sent1 == "q") 
        { 
         goto Break; 
        } 
       } 
      } 
      **if (s2 == 2) 
      { 
       string sent2; 
       Console.WriteLine("Enter several sentences, when done entering" + 
            " sentences, use q by itself on the last line:"); 
       while(true) 
       { 
        sent2 = Console.ReadLine(); 
        if(sent2 == "q") 
        { 
         // Console.WriteLine("There are " + + " words in that text"); 
         break;** 
        } 
       } 
      } 
     } 
    } 
    static int Def1(string d1 = "Hello") 
    { 
     int countWords = 0; 
     for (int i = 1; i < d1.Length; i++) 
     { 
      if (char.IsWhiteSpace(d1[i - 1]) == true) 
      { 
       if (char.IsLetterOrDigit(d1[i]) == true || 
        char.IsPunctuation(d1[i])) 
       { 
        countWords++; 
       } 
      } 
     } 
     if (d1.Length > 2) 
     { 
      countWords++; 
     } 
     return countWords; 

    } 
    static int Def1(string[] d1) 
    { 
     var e = from a in d1 select a; 
     int cnt1 = e.Count(); 

     return cnt1; 
    } 
    static int vow1(string v1) 
    { 
     char[] vowels = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' }; 
     int total = 0; 

     for(int i = 0; i < v1.Length; i++) 
     { 
      if(vowels.Contains(v1[i])) 
      { 
       total++; 
      } 
     } 
     return total; 
+0

岂不是更好的方法使用ReadKey并将其追加到一个字符串,如果它不是q? –

+0

任何方式,你可以给我一个例子,可能看起来像@rohit我想这样做,但没有得到我期待的结果。 –

回答

0

对于几串计数的单词数(段),你可以这样写

if (s2 == 2) //2. Count the word in a paragraph 
{ 
    string senttence2; 
    List<string> sentences = new List<string>(); 
    Console.WriteLine("Enter several sentences, when done entering" + 
         " sentences, use q by itself on the last line:"); 
    senttence2 = Console.ReadLine(); 
    while (senttence2 != "q") 
    { 
     sentences.Add(senttence2); 
     senttence2 = Console.ReadLine(); 
    } 
    Console.WriteLine("There are " + GetWordCount(sentences) + " words in that text"); 
} 

的代码,这是(使用Linq

static int GetWordCount(IEnumerable<string> sentences) 
{ 
    return sentences.Select(s => s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count()).Sum(); 
} 
+0

非常有帮助。非常感谢你@Arghya。 –

+0

@E_T_A如果答案对您有帮助,请考虑接受答案。 –

0

首先可以是这样的。

您需要两个变量。第一个是保存用户输入的临时值。其值与q内部while循环进行比较。直到临时值不等于q,我们应该继续循环并将temp的值附加到必须包含所有输入的另一个字符串(除去最后的q除外)。

if (s2 == 2) 
{ 
    string temp = ""; 
    Console.WriteLine("Enter several sentences, when done entering" + 
         " sentences, use q by itself on the last line:"); 
    string finalString = ""; // initial empty value 
    while((temp = Console.ReadLine()) != "q") // assign input to temp and check its value 
    { 
     finalString += " " + temp; // temp was not `q` so append it to text. 
    } 
    Console.WriteLine(???); // explained later 
} 

请注意,我们添加了额外的空间,每个附加tempfinalstring确保词在一行的末尾分离和下一行的开始。

什么应该在Console.WriteLine(???);

这可以用Split方法很容易地完成。您可以用空格和逗号分隔finalstring。然后检查它是否包含单词。

var possibleWords = finalString.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries) 

RemoveEmptyEntries仅仅是从分割的结果删除空项目的选项。

现在您必须检查每个字符串项目是否包含字母。如果他们包含字母,我们把它算作单词。

var letterCount = possibleWords.Count(x => x.Any(char.IsLetter)); 

的方法Count计数时有史以来x => x.Any(char.IsLetter)返回true。

x.Any(char.IsLetter)只要字符串x包含至少一个字母就会返回true。

所以你可以打印这样的东西。

Console.WriteLine("There are " + letterCount + " words in that text"); 

更好的方法来计算信件是使用的正则表达式但那比你更高的水平,所以我不想把它放在这里。希望能帮助到你。

相关问题