C# 中包含至少六个单词的字符串的 While 循环

While loop of a string containing at least six words in C#

我正在尝试编写 while 循环验证代码,以在用户输入符合以下条件的句子时验证用户的响应:

我能够让 null 或 empty 条件按预期工作,但 "must be at least six words" 目前没有按预期工作。每当我输入少于六个单词的句子时,它都能接受。但是,如果我输入一个包含六个或更多单词的句子,它会提示已建立的错误消息。

        while (String.IsNullOrEmpty(sentence) || sentence.Length != 6)
        {
            if (String.IsNullOrEmpty(sentence))
            {

                Console.WriteLine("Please, do not leave the sentence field empty!");
                Console.WriteLine("Enter your desired sentence again: ");
                sentence = ReadLine();
            }
            else
            {
                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: ");
                sentence = ReadLine();
            }
        }

我到底做错了什么?

while (String.IsNullOrEmpty(sentence) || sentence.Length != 6)改为

while (String.IsNullOrEmpty(sentence) || sentence.Split(' ').Length < 6)
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();
        }

sentence.Length returns 字符串中的字符数。您必须将句子拆分成单词。

string[] words = sentence.Split();

在白色-space 个字符处拆分。

因此,您可以将循环编写为

while (String.IsNullOrEmpty(sentence) || sentence.Split().Length < 6)
{
    ...
}

这里Length是拆分后的字符串数组的长度。

注意如果语句是null,C#的布尔表达式short-circuit evaluation不会执行||后面的子表达式。因此你不会得到空引用异常。

首先你可以在如下情况下改变你... 它会给你一个长度小于六的句子 而 (sentence.Length < 6) 当你想得到一个长度为六个单词的单词时,请尝试以下条件...

sentence.Split(' ').Length >= 6

// 首先尝试像下面这样改变 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();
    }

试试下面的示例代码

 string sentence=Console.ReadLine();

            if (String.IsNullOrEmpty(sentence))
            {

                Console.WriteLine("Please, do not leave the sentence field empty!");
                Console.WriteLine("Enter your desired sentence again: ");
                sentence = Console.ReadLine();
            }
            else if(sentence.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: ");
                sentence =  Console.ReadLine();
            }
            else
            {
            Console.WriteLine("Your entered string length is'{0}' and word is{1}", sentence.Length,sentence);
            }