【问题标题】:Do while problems when trying to loop if condition is not attain如果条件未达到,则在尝试循环时执行 while 问题
【发布时间】:2014-08-11 17:22:02
【问题描述】:

我正在尝试通过输入一个介于 10-50 之间的整数来让我的程序工作,如果他们没有在可接受的范围内输入,那么循环将通过让他们再次输入来返回。但我似乎无法理解为什么我的程序不起作用。我知道背后的逻辑,但我认为代码是问题所在。这是我的代码

Console.WriteLine("Enter a digit between 10 and 50 ");
xx = Console.ReadLine();
x = int.Parse(xx);
do
{
    if (x > 10 && x < 50)
        Console.WriteLine("Pleae input again: ");
}
while (x <= 10 && x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();

【问题讨论】:

  • 将 (x &lt;= 10 &amp;&amp; x &gt;= 50) 更改为 (x &lt;= 10 || x &gt;= 50)
  • 问题是你没有在循环内抓取输入。一旦进入循环,变量 x 就永远不会改变。 (除了三叶草说的)
  • 但我猜 && 不会有所作为,因为它将数字限制在 10 到 50 之间。
  • @user3806140:是的,它会的。 x 不能小于 10 且大于 50 ;)

标签: c# loops if-statement do-while


【解决方案1】:

if 条件错误,while 条件错误!

试试这个:

Console.WriteLine("Enter a digit between 10 and 50 ");
do
{
    xx = Console.ReadLine();
    x = int.Parse(xx);
    if (10 <= x && x <= 50)
        break;
    Console.WriteLine("Pleae input again: ");
}
while (true);

【讨论】:

  • 2 件事,不是错就是更错... 2 号 OP 还应该在另一个 if 语句中使用 tryParse 进行输入验证,否则当他们输入字符而不是数字时会抛出错误。 .. 除非他们希望它抛出错误,否则没关系:)
  • @developerIntern53718:谢谢你的英语课。我并不是说一个比另一个“更错误”,只是想强调整个逻辑通常是不正确的。
  • @developerIntern53718:关于异常问题,由于OP的代码没有在方法中给出,所以没有办法告诉它,因此没有必要解决这个问题。
  • 这是有道理的,我知道他们没有要求它可能是因为他们不知道要它虽然......
  • @user3806140:这意味着你有一段代码在运行时永远不会到达。如果您在理解那段代码的确切位置时遇到问题,请在单独的问题中发布,因为我很难在没有看到实际代码的情况下回答它。
【解决方案2】:

这个怎么样:

    string xx;
    int x;
    Console.WriteLine("Enter a digit between 10 and 50 ");
    bool cont = true;
    do
    {
        xx = Console.ReadLine();
        if (int.TryParse(xx, out x) && 10 <= x && x <= 50)
            cont = false;
        else
            Console.WriteLine("Pleae input again: ");
    }
    while (cont);

看到while(true) 让我毛骨悚然。而且,您应该始终使用int.TryParse 而不是int.Parse 进行用户输入。

【讨论】:

  • 改为使用while(true); 并使用break 语句。然后删除else。这会将其缩小 2 行。 :)
  • 是的,你很搞笑。而且,它会缩小 2 行(删除 cont 变量声明和其他)——就是这样。 cont=false 将被替换为中断。
  • while(true) 是一种糟糕的代码结构。应尽可能避免使用 break 语句。我也是认真的。
  • 谁教你的?只要你知道如何正确使用break 语句,那么使用它们就完全没问题了。其实这个网站上有一个主题..
  • 在这样的小循环中——我想这并不可怕;但是,它会使更复杂代码的可读性和可追溯性变得困难。并且 - 在我上过的几乎所有软件开发课程中都重申了这一点。我不太关心多 2 行代码,而更关心代码的可维护性。
【解决方案3】:

检查您的 IF 和 WHILE 条件。试试这个:

Console.WriteLine("Enter a digit between 10 and 50 ");

do
{
    xx = Console.ReadLine();
    x = int.Parse(xx);
    if (x <= 10 || x >= 50)
        Console.WriteLine("Pleae input again: ");
}
while (x <= 10 || x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();

【讨论】:

  • 这是我的第一个代码,它不起作用,只是给我检测到无法访问的代码。
【解决方案4】:

您需要每次都要求输入,否则您将无法退出循环。

    do
     {
      xx = Console.ReadLine();
      x = int.Parse(xx);  
      if (x > 10 && x < 50)
          Console.WriteLine("Pleae input again: ");
     }
    while (x <= 10 && x >= 50);
    Console.WriteLine("The number is in between!");
    Console.Read();

【讨论】:

  • 他仍然不会脱离循环。当x 小于 10 且 x 大于 50 时它将循环...
  • 这对 x 的值进行了冗余检查 - 不是很理想。
猜你喜欢
  • 2023-03-07
  • 2017-12-14
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 2015-07-18
  • 1970-01-01
相关资源
最近更新 更多