c# - Comparing a string to another string -


i have user's response regarding age stored int age;. ask user divide age number , input number console. number stored int rep;. there, want ask if int rep; or odd number. realize cannot use string in if () statement. however, have not able formulate question find solution/post on web me understand how check if user's input store string can compared answer expect.

to sum: there equivalent syntax if () string?

//second task int age; int rep;   console.writeline ("how old you? "); age = convert.toint32 (console.readline ()); console.writeline ("what age divided first number submitted in previous question? "); rep = convert.toint32 (console.readline ()); if (rep == age / num01 ) {     console.writeline ("that correct. proceed next question. "); } else {     console.writeline ("that incorrect. start over. "); } console.writeline ();  //third task string ans;  console.writeline ("is answer previous question or odd number? "); ans = console.readline (); if (rep % 2 == 0 && ans == even) {     console.writeline ("that correct. "); } if (rep % 2 == 1 && ans == odd)  {     console.writeline ("that correct. "); }    

in case change

ans = console.readline(); 

to this.

ans = console.readline().tolower(); 

then change ites this.

if (rep % 2 == 0 && ans == "even") {//code here.} else if (ans == "odd") {//code here.} // should else if not if. being else if check rep % 2 == 1 not needed. 

alternatively better string comparison should this.

ans = console.readline(); if (rep % 2 == 0 && ans.equals("even", stringcomparison.ordinalignorecase)) {//code here.} else if (ans == ans.equals("odd", stringcomparison.ordinalignorecase)) {//code here.} // should else if not if. being else if check rep % 2 == 1 not needed.  

the above check comparison , ignore case of string not need use tolower , problems string comparisons using == should not arise.

thanks alexei levenkov pointing out.

you can check out future references on string comparisons. https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -