c# - checking that a number is within range -


am trying make mastermind game using array of ints having user guess number sequence between 4-10 instead of colours. getavalidnumber suppose displays prompt user , number user in minimum/maximum range specified in parameters. if number entered outside minimum/maximum range, error message suppose displayed , user re-prompted number reason isn't validating properly.

any guidance appreciated

    public static int getavalidnumber(string inputmessage)     {         // declare variables         int validinteger;         bool inputisvalid = false;         int lowvalue = 1;         int highvalue = 10;         while (!int.tryparse(console.readline(), out validinteger))         {             console.writeline("invalid entery - try again.");             console.write(inputmessage, lowvalue, highvalue);                         {                 while (!int.tryparse(console.readline(), out validinteger))                 {                     console.writeline("invalid entery - try again.");                     console.write(inputmessage);                 }                 if (validinteger < lowvalue || validinteger > highvalue)                 {                     console.writeline("your number out of range: ");                 }                 else                 {                     inputisvalid = true;                 }              } while (validinteger < lowvalue || validinteger > highvalue);         }         return validinteger;     }      // method directs play of game     public static void playgame()     {         int usernumbers;         int userguess;         int difficulty;         int randomnumbers;          console.writeline("how many numbers use when playing(4-10): ");         usernumbers = getavalidnumber(console.readline());          console.writeline("choose difficulty level (1 -3");         difficulty = getavalidnumber(console.readline());         console.writeline("a 5-digit number has been chosen. each possible digit may number 1, 2, 3, or 4");         console.readline();         console.writeline("enter guess {0} guesses remaning", getgamedifficulty(difficulty, randomnumbers = getrandomnumbercount(difficulty)));         userguess = getavalidnumber(console.readline());         console.writeline("your guess {0}, results {1}", userguess, counthits(userguess, randomnumbers));         console.readline();      } 

here's simplified version of code:

    public static int getavalidnumber(string inputmessage)     {         int validinteger = 0;          //range between 4 10         int lowvalue = 4;          int highvalue = 10;          int.tryparse(inputmessage, out validinteger);          while (validinteger < lowvalue || validinteger > highvalue)         {             console.writeline("invalid entery - try again.");             inputmessage = console.readline();             int.tryparse(inputmessage, out validinteger);         }         return validinteger;      } 

the issue here having multiple while loops not validating inputs correctly.

take note on other validating number inputs need different ranges suggest setting lowvalue , highvalue parameters on function:

public static int getavalidnumber(string inputmessage, int lowvalue, int highvalue) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -