// 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);
}
答案 0 :(得分: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");
因此,只有在您的句子有效时才计算字符数。否则只打印一条消息。
答案 1 :(得分: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