1
 // If statement, User input is executed
 if (userInput == "k")
 {
   // User enters their statement
   Console.WriteLine("Enter your statement");
   myScen = Convert.ToString(Console.ReadLine());

句号代码

   // Does the sentence end with a full stop?
   if (myScen.EndsWith("."))
      Console.WriteLine("\n\tSentence Ended Correctly");

   else
      Console.WriteLine("Invalid Sentence");

这里需要一个停止命令。这是它不会停止并继续计算数字的地方

   // Calculate number of characters
   foreach (char c in myScen)
   {
      numbChar++;
      if (c == ' ')
         continue;

      newScen += c;
   }
   Console.WriteLine("\n\tThere are {0} characters. \n\n\n",numbChar);
}
4

2 回答 2

1

让我试试:

if (myScen.EndsWith("."))
{
    Console.WriteLine("\n\tScentence Ended Correctly");
    // Calculate number of characters
    foreach (char c in myScen)
    {
        numbChar++;
        if (c == ' ')
            continue;

        newScen += c;
    }
    Console.WriteLine("\n\tThere are {0} characters. \n\n\n",numbChar);
}
else
    Console.WriteLine("Invalid Scentence");

因此,仅当您的句子有效时才计算字符数。否则只打印一条消息。

于 2012-11-17T15:41:27.320 回答
0

您不需要将Console.ReadLine()调用结果转换为字符串,因为它已经是字符串。

if (userInput == "k")
{
   Console.WriteLine("Enter your statment");
   myScen = Console.ReadLine();

   if (myScen.EndsWith(".")) 
   { // use block of code here
       Console.WriteLine("\n\tScentence Ended Correctly");
       // Calculate number of characters...
   }
   else
       Console.WriteLine("Invalid Scentence");       
}

您也可以通过这种方式计算非空白字符mySen.Replace(" ", "").Length

于 2012-11-17T15:42:02.337 回答