【问题标题】:How can I make sure that a certain line is printed only when the if statement is met in a while-loop如何确保仅在 while 循环中满足 if 语句时才打印特定行
【发布时间】:2019-10-12 15:53:17
【问题描述】:

我正在尝试打印 if 语句,但前提是值正确且符合要求。但是,if 语句位于 while 循环内,即使它要求用户在 if 语句中的值不正确时重试,它仍然会打印出用户第一次在 if 之外的 WriteLine 中输入的数字声明(它说模拟)。我希望它要求用户再试一次,直到值符合 if 语句的要求标准。

namespace Woodchuck
{
    class Program
    {
        static void Main(string[] args)
        {
            string restart = "Y";


            while (restart == "Y")
            {
                int totalWoodChucks = 0;
                int days = 0;

                const int MAXDAYS = 10;
                const int MINDAYS = 1;
                const int MAXWOOD = 100;
                const int MINWOOD = 1;


                Write("Enter number of woodchucks for this simulation (1-100): ");
                int.TryParse(ReadLine(), out totalWoodChucks);

                if (totalWoodChucks > MAXWOOD || totalWoodChucks < MINWOOD)
                {
                    WriteLine("Please try again");
                }


                Write("Enter number of days for this simulation (1-10): ");
                int.TryParse(ReadLine(), out days);
                if (days > MAXDAYS || days < MINDAYS)
                {
                    WriteLine("Please try again");
                }


                WriteLine($"SIMULATION 1: {totalWoodChucks} woodchucks over {days} days");

                Write("\nTo run another simulation, enter 'Y':");
                restart = ReadLine().ToUpper();

            }
            //Debug pause
            WriteLine("Press any key to wrap it up...");
        }
    }
}

【问题讨论】:

    标签: c# if-statement while-loop


    【解决方案1】:

    字符串重启 = "Y";

        while(restart == "Y")
        {
            int totalWoodChucks = 0;
            int days = 0;
    
            const int MAXDAYS = 10;
            const int MINDAYS = 1;
            const int MAXWOOD = 100;
            const int MINWOOD = 1;
    
    
            Write("Enter number of woodchucks for this simulation (1-100): ");
            int.TryParse(ReadLine(), out totalWoodChucks);
            Write("Enter number of days for this simulation (1-10): ");
            int.TryParse(ReadLine(), out days);
    
            if((totalWoodChucks > MAXWOOD || totalWoodChucks < MINWOOD) &&(days > MAXDAYS || days < MINDAYS))
            {
                WriteLine("Please try again");
                continue;
            }
    
            WriteLine($"SIMULATION 1: {totalWoodChucks} woodchucks over {days} days");
    
            Write("\nTo run another simulation, enter 'Y':"); 
            restart = ReadLine().ToUpper();
    
        }
        //Debug pause
        WriteLine("Press any key to wrap it up...");
    

    我所做的是在收集完所有数据后进行检查。然后如果数据不符合要求,它将再次要求再次尝试,直到数据满足为止。

    【讨论】:

      【解决方案2】:

      您可以通过使用 while 循环来做到这一点,直到满足所需的条件。

      这里有一个简单的例子来说明如何做到这一点:

      int totalWoodChucks = 0;
      int days = 0;
      
      const int MAXDAYS = 10;
      const int MINDAYS = 1;
      const int MAXWOOD = 100;
      const int MINWOOD = 1;
      
      bool validWoodChucks = false;
      while(!validWoodChucks)
      {
          Console.Write("Enter number of woodchucks for this simulation (1-100): ");
          int.TryParse(Console.ReadLine(), out totalWoodChucks);
      
          if(totalWoodChucks > MAXWOOD || totalWoodChucks < MINWOOD)
          {
              Console.WriteLine("Invalid woodchucks. Please try again.");
          }
          else
          {
              validWoodChucks = true;
          }
      
      }
      
      bool validDays = false;
      while(!validDays)
      {
          Console.Write("Enter number of days for this simulation (1-10): ");
          int.TryParse(Console.ReadLine(), out days);
          if (days > MAXDAYS || days < MINDAYS)
          {
              Console.WriteLine("Invalid days. Please try again.");
          }
          else
          {
              validDays = true;
          }
      }
      
      
      Console.WriteLine("Wood chucks: " + totalWoodChucks);
      Console.WriteLine("Days: " + days);
      
      Console.ReadLine();
      

      使用示例:

      Enter number of woodchucks for this simulation (1-100): -10
      Invalid woodchucks. Please try again.
      Enter number of woodchucks for this simulation (1-100): 50
      Enter number of days for this simulation (1-10): 20
      Invalid days. Please try again.
      Enter number of days for this simulation (1-10): 10
      Wood chucks: 50
      Days: 10
      

      【讨论】:

        猜你喜欢
        • 2014-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        相关资源
        最近更新 更多