c#如何在代码中添加约束

时间:2014-12-30 10:15:33

标签: c#

我已经编写了以下代码来计算基于用户年龄的公园入场费总额。代码运行得很完美。我想添加一个功能,如果用户输入年龄的值< 0;为用户提供2个更多机会输入正确的年龄值(年龄值必须> = 0)。 3次尝试后将出现错误消息“无效...”,程序将打印谢谢并终止。我不需要有人编写代码,我需要知道在我的代码中我可以在何处以及如何实现此功能。

static void Main(string [] args)         {

        string username;
        int password, count=0 , num_guest , age;
        for (int i = 0; i < 3; i++)
        {
            Console.Write("Username : ");
            username = Console.ReadLine().ToString();
            Console.Write("Password : ");
             password = Convert.ToInt32(Console.ReadLine());

             if (password.Equals(63100))
             {
                 Console.Clear();
                 Console.WriteLine("Welcome " + username);
                 Console.Write("Enter number of guests : ");
                 num_guest = Convert.ToInt32(Console.ReadLine());

                 if (num_guest <= 0)
                 {
                     Console.Clear();
                     Console.WriteLine("Thank You");
                     Environment.Exit(0);
                 }
                     else
                 {

                     for (int j = 1; j <num_guest+1; j++)
                     {
                         Console.Write("Enter age of "+j+" guest : ");
                         age = Convert.ToInt32(Console.ReadLine());

                             if (age >= 0)
                             {
                                 if (age == 0 && age == 1)
                                 {
                                     count = count + 0;
                                 }
                                 else if (age >= 2 && age <= 17)
                                 {
                                     count = count + 60;
                                 }

                                 else if (age >= 18 && age <= 59)
                                 {
                                     count = count + 35;
                                 }

                                 else if (age > 60)
                                 {
                                     count = count + 30;
                                 }
                               }

                     }

                     Console.WriteLine(count);
                     Console.ReadLine();

                 }

                 break;
             }
             else
             {
                 Console.WriteLine("Incorrect Password!");
             }
        }

        Console.Clear();
        Console.WriteLine("Bye..." );
        Environment.Exit(0);

    }

5 个答案:

答案 0 :(得分:0)

使用带有计数器的while循环,当它达到0时终止或在用户输入有效年龄时爆发

答案 1 :(得分:0)

您应该将循环与计数器一起使用,每次age <= 0递增计数器。 在循环开始时,检查计数器是否为3,然后打印错误消息并退出。

否则,如果一切正常,break;循环中的while

int counter = 0;
while (true)
{
    if (counter == 3) {
        Console.WriteLine("Thank You");
        Environment.Exit(0);
    }

    age = Convert.ToInt32(Console.ReadLine());
    if (age > 0)
        break;
    counter++;
}

答案 2 :(得分:0)

static void Main(string[] args) 
{
    string username;
    int password, count=0 , num_guest , age;
    for (int i = 0; i < 3; i++)
    {
        Console.Write("Username : ");
        username = Console.ReadLine().ToString();
        Console.Write("Password : ");
         password = Convert.ToInt32(Console.ReadLine());

         if (password.Equals(63100))
         {
             Console.Clear();
             Console.WriteLine("Welcome " + username);
             Console.Write("Enter number of guests : ");
             num_guest = Convert.ToInt32(Console.ReadLine());

             if (num_guest <= 0)
             {
                 Console.Clear();
                 Console.WriteLine("Thank You");
                 Environment.Exit(0);
             }
                 else
             {
                 //make counter here 
                 for (int j = 1; j <num_guest+1; j++)
                 {
                     Console.Write("Enter age of "+j+" guest : ");
                     age = Convert.ToInt32(Console.ReadLine());

                         if (age >= 0)
                         {
                             //reset counter here
                             if (age == 0 && age == 1)
                             {
                                 count = count + 0;
                             }
                             else if (age >= 2 && age <= 17)
                             {
                                 count = count + 60;
                             }

                             else if (age >= 18 && age <= 59)
                             {
                                 count = count + 35;
                             }

                             else if (age > 60)
                             {
                                 count = count + 30;
                             }
                         }
                         else
                         {
                            // minus 1 of j  increment counter here if counter hits 3 then ouput message then terminate program
                         }

                 }

                 Console.WriteLine(count);
                 Console.ReadLine();

             }

             break;
         }
         else
         {
             Console.WriteLine("Incorrect Password!");
         }
    }

    Console.Clear();
    Console.WriteLine("Bye..." );
    Environment.Exit(0);

}

答案 3 :(得分:0)

阅读并解析年龄输入后,您可以使用while循环对其进行验证,如果无效则再次询问。

Console.Write("Enter age of "+j+" guest : ");
age = Convert.ToInt32(Console.ReadLine());
int tries = 0;
while (age < 0)
{
    if (tries >= MAX_TRIES)
       return;
    else
       ++tries;

    Console.WriteLine("Age must be zero or a positive number");
    age = Convert.ToInt32(Console.ReadLine());
}

答案 4 :(得分:0)

首先,您应该开始将代码分解为单独的方法以提高可读性。如果用户未在3次尝试中输入有效年龄,则可以将其命名为ReadValidAge并返回-1。类似的东西:

 private static int ReadValidAge(int guestNumber)
 {
      int result;
      for(int i = 3; i > 0;)
      {
          Console.Write("Enter Age of guest {0}: ", guestNumber);
          if(int.TryParse(Console.ReadLine(),out result) && result > 0)
          {
              return result;
          }
          else
          {
               Console.WriteLine("Invalid Age entered ... {0} Tries left!", --i); 
          }
      }
      return -1;
 }

然后你调用它并用程序中的if语句分支:

  for (int j = 1; j <num_guest+1; j++)
  {
      age = ReadValidAge(j);
      if(age == -1)
      {
          //Say byebye here

          return;
      }
      // your orignal code