Difference between revisions of "Validation for menu options for the coordinate input"
(Created page with "==Checking coordinates== the InputCoordinate method is within the Simulation class, see below: <syntaxhighlight lang=csharp line> private int InputCoordinate(char Coordinate...") |
(→Checking coordinates) |
||
Line 1: | Line 1: | ||
==Checking coordinates== | ==Checking coordinates== | ||
− | + | The InputCoordinate method is within the Simulation class, see below: | |
<syntaxhighlight lang=csharp line> | <syntaxhighlight lang=csharp line> | ||
Line 12: | Line 12: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So: | This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So: |
Revision as of 22:52, 13 February 2017
Checking coordinates
The InputCoordinate method is within the Simulation class, see below:
1 private int InputCoordinate(char Coordinatename)
2 {
3 int Coordinate;
4 Console.Write(" Input " + Coordinatename + " coordinate: ");
5 Coordinate = Convert.ToInt32(Console.ReadLine());
6 return Coordinate;
7 }
This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So:
1 private int InputCoordinate(char Coordinatename)
2 {
3 do
4 {
5 Console.Write(" Input " + Coordinatename + " coordinate: ");
6 Coordinate = Convert.ToInt32(Console.ReadLine());
7 }
8 while (Coordinate < 0 || Coordinate >= LandscapeSize);
9 return Coordinate;
10 }
the Coordinate is invalid if it is less than zero or greater than or equal to the LandscapeSize (ie first element is 0).