3 回答
TA贡献1820条经验 获得超3个赞
string sentence = Console.ReadLine();
while (true)
{
if (String.IsNullOrEmpty(sentence))
{
Console.WriteLine("Please, do not leave the sentence field empty!");
Console.WriteLine("Enter your desired sentence again: ");
}
else if (sentence.Split(' ').Length < 6)
{
Console.WriteLine("\r\nThe sentece entered isn't valid. Must have a least six words!");
Console.WriteLine("Enter a sentence with a least 6 words: ");
}
else break;
sentence = Console.ReadLine();
}
TA贡献1828条经验 获得超3个赞
sentence.Length返回字符串中的字符数。你必须把句子分成单词。
string[] words = sentence.Split();
在空白字符处拆分。
因此,您可以将循环编写为
while (String.IsNullOrEmpty(sentence) || sentence.Split().Length < 6)
{
...
}
这Length是拆分产生的字符串数组的长度。
请注意,如果句子是null,C# 的布尔表达式的短路求值将不会执行||.后面的子表达式。因此,您不会得到空引用异常。
TA贡献1876条经验 获得超5个赞
// 首先尝试改变 while 条件,如波纹管 ....然后尝试波纹管代码..
public static void Main(string[] args)
{
int count = 0;
inputSteream:
Console.WriteLine("Enter your sentence: ");
string sentence = Console.ReadLine();
while (!String.IsNullOrEmpty(sentence) && sentence.Length >= 6)
{
foreach (var item in sentence.Split(' '))
{
if (item.Length >= 6)
{
Console.WriteLine("The sentece is {0}", item);
count++;
break;
}
}
break;
}
if (count == 0)
{
goto inputSteream;
}
Console.ReadKey();
}
- 3 回答
- 0 关注
- 231 浏览
添加回答
举报
