简体   繁体   中英

C# code How to write method for number between 0 to 20

How to add method here to find out the number must be between 0 and 20 . and i want to Use TryParse() method to check that an integer has been entered is between 0 to 20 and that should be but here if i enter 21 that is also working which is wrong please help! here is my code

static void Main(string[] args) {

        int number;
        int a = 0;
        char ec;
        Write("Enter number of participants between 0 to 20 >> ");
        while (a == 0)
        {

            if (int.TryParse(ReadLine(), out number))
            {
                a = 1;
                WriteLine("the expected revenue is " + ComputeRevenue(number).ToString("C"));
                Sport[] player = new Sport[number];

                for (int x = 0; x < number; x++)
                {
                    player[x] = new Sport();
                    Write("Enter Player Name #{0} >> ", x + 1);
                   
                    player[x].EventCode = ReadLine()[0];

                }

You are not checking the value of the number you read. You can check it with a simple if condition.

int number;
        int a = 0;
        char ec;
        Write("Enter number of participants between 0 to 20 >> ");
        while (a == 0)
        {
            
               if (int.TryParse(ReadLine(), out number))
               {
                if(number>=0 && number<20){
                   a = 1;
                   WriteLine("the expected revenue is " + ComputeRevenue(number).ToString("C"));
                   Sport[] player = new Sport[number];

                   for (int x = 0; x < number; x++)
                   {
                       player[x] = new Sport();
                       Write("Enter Player Name #{0} >> ", x + 1);
                   
                       player[x].EventCode = ReadLine()[0];

                   }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM