【问题标题】:do-while loop does not go through while condition C#do-while 循环不通过 while 条件 C#
【发布时间】:2019-11-09 04:21:40
【问题描述】:

我写了一个 do-while 循环,但它并没有以某种方式运行 while 条件。 当我输入无效字符时,它应该回到开头并按预期重复。 我在 Visual Studio 上逐步运行代码,它表明代码甚至没有通过 while 条件。 (不管输入值是多少) 有人可以帮帮我吗? 非常感谢!

using System;
using static System.Console;

namespace a5
{
    class Program
    {
        const string acceptedLetters = "EHLNTXZ";

        static void Main(string[] args)

        {
            GetUserString(acceptedLetters);
            ReadKey();
        }

        static string GetUserString(string letters)
        {
            string invalidCharacters;
            do
            {
                invalidCharacters = null;

                Write("Enter : ");

                string inputCharacters = ReadLine();

                foreach(char c in inputCharacters) 
                {
                    if(letters.IndexOf(char.ToUpper(c))==-1)
                    {
                        invalidCharacters = c.ToString();
                    }
                }

                if(invalidCharacters != null)
                {
                    WriteLine("Enter a valid input");
                }
                return inputCharacters;
            } while (invalidCharacters != null);


         } 
    }
}

【问题讨论】:

  • 你在循环结束时有一个return,为什么你认为它会在循环结束后继续?

标签: c# loops while-loop do-while


【解决方案1】:

问题是无论验证是否完成,您都会在循环结束时返回输入的字符串。

您可以使用布尔值来检查此有效性。

你也不需要解析所有的字符串,你可以在第一个无效字符上打破内循环。

我将字符串重命名为 result 以使用标准模式并更简洁。

例如:

static string GetUserString(string letters)
{
  string result;
  bool isValid;
  do
  {
    Console.Write("Enter : ");
    result = Console.ReadLine();
    isValid = true;
    foreach ( char c in result )
      if ( letters.IndexOf(char.ToUpper(c)) == -1 )
      {
        isValid = false;
        Console.WriteLine("Enter a valid input");
        break;
      }
  }
  while ( !isValid );
  return result;
}

【讨论】:

  • 我没有完全按照您写的内容进行操作,但我想到了在您提供的内容之后返回 while 条件(而不是在“do”内返回)。谢谢奥利维尔!
【解决方案2】:

return inputCharacters; 行使其脱离循环。

我想你的意思是:

} while (invalidCharacters != null);
return inputCharacters; 

【讨论】:

    【解决方案3】:
    using System;
    using static System.Console;
    
    namespace a5
    {
        class Program
        {
            const string acceptedLetters = "EHLNTXZ";
    
            static void Main(string[] args)
    
            {
                GetUserString(acceptedLetters);
                ReadKey();
            }
    
            static string GetUserString(string letters)
            {
                string invalidCharacters;
                do
                {
                    invalidCharacters = null;
    
                    Write("Enter : ");
    
                    string inputCharacters = ReadLine();
    
                    foreach(char c in inputCharacters) 
                    {
                        if(letters.IndexOf(char.ToUpper(c))== -1)
                        {
                            invalidCharacters = c.ToString();
                        }
                    }
    
                    if(invalidCharacters != null)
                    {
                        WriteLine("Enter a valid input");
                    }
    
                } while (invalidCharacters != null);
                return inputCharacters;
             } 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2018-05-09
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多