Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place herehere.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

quoted original code, it is mainly for reference
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 
while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed in the first place here.

Original Code

while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); try { playerChoice = Convert.ToInt32 (Console.ReadLine()); } catch (FormatException e) { validInput = false; } if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } else { validInput = false; } } 

Here is what I came up with based on our conversation.

New Code

var validInput = false; //forgot this part while (!validInput) { var playerChoice = -1; Console.WriteLine("Please choose your Gesture "); gameSetup(listOfGestures); validInput = int.TryParse(Console.ReadLine(),out playerChoice); if (playerChoice > 0 && playerChoice <= listOfGestures.Count) { playerGesture = listOfGestures[playerChoice - 1]; validInput = true; } } 

Do I need to create a new method to sort out user input and whether or not it is an integer inside the min and max bounds?

edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Is my coding technique progressing Ensuring user input is an integer in terms of C# loops?a range

Tweeted twitter.com/#!/StackCodeReview/status/443698802575568896
added 48 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
Loading
deleted 59 characters in body; edited tags; edited title
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312
Loading
deleted 59 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
Loading