c# - How can I write (userInput >= "50") properly? -


i mean "if number typed user greater 50 then..." how can write properly? because error shows in visual studio:

operator '>=' cannot applied operands of type 'string' , 'string'

console.write("enter number: ");         string userinput = console.readline();         string message = (userinput >= "50") ? "your number greater 50" : "you number less 50";          console.writeline(message);         console.readline(); 

strings not numbers, parse it:

int userinputnum = 0; string userinput = console.readline();  if (int.tryparse(userinput, out userinputnum)) {     string message = (userinputnum > 50) ? "your number greater 50" : "you number less 50";     console.writeline(message); } else {     //junk user input } 

note can use int.parse instead, throw if user inputs non-number. out keyword in second argument forces called function populate argument before returning (used because signature of tryparse calls it). also, logic greater or equal to 50. code above strictly greater than.

your original code doesn't work because comparing user input string (hence "cannot applied operands of type 'string' , 'string'") because have compared "50". string cannot "greater than" or "less than" string, "equal to" (==) or "not equal to" (!=).

more specifically, > operator not defined on string , cannot used compare 2 of them.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -