【问题标题】:Do loop practice?做循环练习吗?
【发布时间】:2015-01-02 02:34:05
【问题描述】:
        static void Main(string[] args)
        {
            int output = 0;
            int number = 0;
            Console.WriteLine("Please input a number for it to be counted!");
            bool conversion = int.TryParse(Console.ReadLine(), out output);
            if( number >= 1000)
            {
                while (number <= output)
                {
                    switch (conversion)
                    {
                        case true:
                            Console.Write(number + " ");
                            number += 2;
                            break;
                        case false:
                            Console.WriteLine("ERROR INVALID INPUT!");
                            break;

                    }
                }
            }
            else
            {
                Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW OR AT 1000 TO PREVENT OVERFLOW!");
            }
            string choice = Console.ReadLine();
          do                                        // Here is the beginning of the do code
          {
              Console.WriteLine("Do you want to continue - Yes or No");
              if(choice.ToUpper() != "NO" && choice.ToUpper() != "NO");
              {
                  Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");
              }
          }while(choice.ToUpper() != "YES" && choice.ToUpper() != "NO");
        }
    }
}

这是一个非常简单的应用程序,尚未完成,但它的作用是输入一个小于或等于 1000 的数字,它会均匀地计数。我唯一的问题是代码末尾的 do 语句。它尚未完成,但根据我的研究,do 语句已完成,如果用户没有输入是或否,它会向他们显示该错误,然后再次询问“你想再去一次吗”但是因为这个 do 语句我输入了 10 之类的任何数字,它给了我一个错误,说它超过 1000,然后继续说“错误无效输入:只有输入是或否!”的无限循环。我该如何解决这个问题?

【问题讨论】:

  • 哇,在循环内的布尔值上使用switch() 语句。壮观的 WTF。

标签: c# loops while-loop


【解决方案1】:

你的 if 语句测试 NOT EQUAL 到 "no" 两次,我假设你的意思是检查不等于 "YES" 和不等于 "NO"

【讨论】:

    【解决方案2】:

    您的无限循环是因为条件检查了 choice 的内容,但您永远不会在 do 循环中更改变量的值 - 仅在循环开始前的 Console.ReadLine() 中。

    您应该在要重复的代码之前启动 do 循环。例如,如果您的意图是如果用户键入是整个过程重复,则最好通过创建一个 void 函数来为您服务,例如 static void CountNumbers() { ... },其中所有代码都在执行您输入的计数、错误处理等。然后您可以使用 do 循环轻松地重复用户想要的次数,如下所示:

    string choice = null;
    do
    {
        CountNumbers();
        // Your code here to ask if user wants to continue and readline to get the new choice
    } while ( /* Existing condition */);
    

    【讨论】:

      【解决方案3】:

      代码中有几个问题:

      首先,您的条件检查number 是否大于或等于1000。由于您输入的是10,而不是&gt;= 1000,因此您将进入else 语句。因此,您需要将您的条件更改为:

      if(number <= 1000)
      

      接下来,do 循环中的条件检查相同的条件两次...您不允许用户输入“是”。您应该修改条件以包括对“是”的检查。此外,您需要从同一 if 语句的末尾删除分号:

      do
      {
          Console.WriteLine("Do you want to continue - Yes or No");
          choice = Console.ReadLine(); 
      
          if(choice.ToUpper() != "NO" && choice.ToUpper() != "NO");
          {
              Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");            
          }
      } while(choice.ToUpper() != "YES" && choice.ToUpper() != "NO");
      

      您还可以考虑添加一个从用户那里获取整数的方法,因为将其包装在一些验证中很方便:

      public static int GetIntFromUser(string prompt,
          string errorMessage = "Invalid entry. Please try again.")
      {
          int value;
      
          while (true)
          {
              if (prompt != null) Console.Write(prompt);
              if (int.TryParse(Console.ReadLine(), out value)) break;
              if (errorMessage != null) Console.WriteLine(errorMessage);
          }
      
          return value;
      }
      

      然后,您可以在需要从用户那里获取 int 时调用此方法。以下是如何使用它的示例:

      private static void GenericTester()
      {
          // This will flag that the user wants to quit
          bool quit = false;
      
          while(!quit)
          {
              // Get input from user
              int userInput = GetIntFromUser("Please input a number to be counted: ");
              int number = 0;
      
              // Write out the numbers
              if (userInput <= 1000)
              {
                  while (number <= userInput)
                  {
                      Console.Write(number + " ");
                      number += 2;
                  }
              }
              else
              {
                  Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW " +
                      "OR AT 1000 TO PREVENT OVERFLOW!");
              }
      
              // See if they want to continue
              while (true)
              {
                  // Get user input
                  Console.Write("Do you want to continue - Yes or No: ");
                  string choice = Console.ReadLine();
      
                  // If they said 'No', set flag to quit and break out of this loop
                  if (choice.Equals("No", StringComparison.OrdinalIgnoreCase))
                  {
                      quit = true;
                      break;
                  }
      
                  // If they said 'Yes', just break out of this loop
                  if (choice.Equals("Yes", StringComparison.OrdinalIgnoreCase)) break;
      
                  // Otherwise, display an error message and let the loop continue
                  Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");
              }
          }
      }
      

      【讨论】:

      • 好的,我已经解决了这个问题,它计算了 numebr,但之后当我按下任何东西时,它又回到了无限循环!当我输入数字并对其进行计数时,它会给我留下空白空间,我无事可做,只能按下一个键,当我按下它时,它会发送垃圾邮件“错误无效输入!”编辑:我修复它我不小心放置了字符串选择 = Console.Readline();消息上方。无论如何,我输入是或否,它告诉我“错误无效输入!!”是什么原因造成的?
      • 您还必须再次从用户那里获得“选择”(在 do 循环内)。我已经更新了示例。
      • 非常感谢。我还设法通过纠正我糟糕的编程位置来修复其他一些错误。但是,如果我输入的不是是或否,它现在会向我发送无效输入是或否的垃圾邮件。它还告诉我错误无效输入请输入是或否!即使我把那个!编辑:好的,所以我现在设法让它工作,剩下的就是解释。你能告诉我为什么我必须重新声明选择是 Console.Readline(); ?
      • 因为每次循环时都需要为“选择”分配一个新值,直到获得所需的值。我已经用可能有帮助的其他示例更新了答案。
      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      相关资源
      最近更新 更多