I’m developing a guess number in ASP.NET and send a guessed number from a textbox to this method:
public Outcome MakeGuess(int guess)
{
//Most of this code can be wrong
if (Number > 1 && Number < 100)
{
foreach (int number in PreviousGuesses)
{
}
if (Number == guess)
{
return Outcome.Correct;
}
else if (Number < guess)
{
return Outcome.High;
}
else if (Number > guess)
{
return Outcome.Low;
}
}
else
{
throw ArgumentOutOfRangeException;
}
And have an Enum who looks like this:
enum Outcome
{
Indefinite,
Low,
High,
Correct,
NoMoreGuesses,
PreviousGuess
}
I should find out if the guess is too low, too high, right guess, an earlier made guess (checking from a list), or if the user has used all tries(checking a constant). I Started trying but i’m stuck! My code may be wrong.
Store your guesses in a list
After calling
MakeGuess(guess)add the guess to the listInstead of the
foreachloop do thisThe last
elsecan be simplified fromto
since this is the only possible case remaining.
I would simplify the method further
The
elsekeyword is not necessary because thereturnstatements end the method execution if a case applies.The error handling should not be necessary if you calculate the random numbers correctly. You should not trap programming errors with error handling; instead, correct the errors! Error handling makes sense when calling a method can lead to an exception. An example is opening a file that might not exist or might be locked or the like. In libraries used by other persons, it makes sense to check if the parameters passed to a method are correct and to throw an exception if not.