Difference between revisions of "2022 - You cause an exception if you enter a card number out of the range 1-5"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Issue= When you are asked to enter a card number you can enter invalid values (ie not 1 to 5). The GetCardChoice method of the BreakThrough class is what gets the value from...")
 
(Issue)
Line 17: Line 17:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
This method currently will ignore and non integer values entered.
+
This method currently will ignore any non integer values entered.
  
 
=What you need to do=
 
=What you need to do=

Revision as of 11:41, 14 November 2021

Issue

When you are asked to enter a card number you can enter invalid values (ie not 1 to 5). The GetCardChoice method of the BreakThrough class is what gets the value from the user, this is the code below:

        private int GetCardChoice()
        {
            string Choice;
            int Value;
            do
            {
                Console.Write("Enter a number between 1 and 5 to specify card to use:> ");
                Choice = Console.ReadLine();
            }
            while (!int.TryParse(Choice, out Value));
            return Value;
        }

This method currently will ignore any non integer values entered.

What you need to do

  • this method should test if value is >=1
  • this method should test if values is <=5
  • this method should still not accept non integer values