0

Hi i am doing very basic c# coding where just checking odd and even number however how i can re-enter input while i am converting a string into integer at one place.

 if (i % 2==0) { Console.WriteLine("even"); Console.WriteLine("enter again", i); } else if(i%2!=0) { Console.WriteLine("odd"); Console.WriteLine("enter again", i); enter code here } Console.ReadKey(); 
3
  • 1
    Console.Write("enter"); i = int.Parse(Console.ReadLine()); Commented Mar 17, 2016 at 12:13
  • 1
    If you have more information to put in your question you should put it into the question and not into a comment. Commented Mar 17, 2016 at 12:20
  • You do not need to have that condition in else again since your "if" already checks for == 0 so only time it comes to else is when its != so checking that again is redundant. Unless there is something else which I'm missing. Commented Mar 17, 2016 at 12:22

6 Answers 6

3

Basically you need a loop and you could make it stop when the user enters a non-integer value.

int i; Console.WriteLine("enter a number"); while(int.TryParse(Console.ReadLine(), out i)) { Console.WriteLine(i%2 == 0 ? "even" : "odd"); Console.WriteLine("enter again"); } 
Sign up to request clarification or add additional context in comments.

Comments

3

You want to do something like that ?

static void Main(string[] args) { var l = string.Empty; while (l != "exit") { l = Console.ReadLine(); int i; if (!int.TryParse(l, out i)) continue; Console.WriteLine(i%2 == 0 ? "even" : "odd"); Console.WriteLine("enter again"); } Console.ReadLine(); } 

4 Comments

Great but..why the else if? i % 2 is 0 or not 0 :)
Also there's no need for passing i in Console.WriteLine("enter again", i); since there are no format items in the string.
Thx but I wanted to keep the closest code as the OP post (I edited my answer). Anyway juharr's answer is the best one
You probably should at least write out something when the user enters a non-integer value that isn't "exit".
0
static void Main() { string userChoice,number; int checkInt; Console.WriteLine("Do you want check even/odd number?y/n"); userChoice = Console.ReadLine(); if (userChoice.ToLower().Equals("y")) { do { Console.WriteLine("Please enter your number"); number = Console.ReadLine(); if (int.TryParse(number, out checkInt)) { if ((checkInt % 2) == 0) { Console.WriteLine("Your entered number {0} is even", checkInt); } else { Console.WriteLine("Your entered number {0} is odd", checkInt); } } else { Console.WriteLine("Plesae enter integer value"); } Console.WriteLine("Do you want check even/odd number?y/n"); userChoice = Console.ReadLine(); } while (userChoice.ToLower().Equals("y")); } } 

Comments

0

I suppose you need a loop that ask you to insert a new number until it is convertible into a integer number.

I suggest you this possible solution

 static int ReadInput(string message) { int n = 0; do { Console.WriteLine(message); } while (!int.TryParse(Console.ReadLine(), out n)); return n; } static void Main(string[] args) { int i = ReadInput("Enter a Number"); if (i % 2==0) { Console.WriteLine("even"); } else if(i%2!=0) { Console.WriteLine("odd"); //enter code here } } } 

Comments

0

In order to convert String into int for a even odd program, the following code can work out -

static void Main(string[] args) { int i; Console.Write("Enter a Number : "); i = int.Parse(Console.ReadLine()); if (i % 2 == 0) { Console.Write("Entered Number is an Even Number"); Console.Read(); } else { Console.Write("Entered Number is an Odd Number"); Console.Read(); } } 

Comments

0

What you want is a really fast method:

If (rowNum % 2 == 0) Print even row color info 

Else Print odd row color info

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.