mardi 7 juillet 2015

C# - comparing a string to other string

I have a user's response regarding their age stored as int age;. I then ask the user to divide their age by some number, and they input that number into my console. That number is stored as int rep;. From there, I want to ask if int rep; is an even or odd number. I realize that you cannot use a string in an if () statement. However, I have not be able to formulate my question properly to find a solution/post on the web that will help me understand how to check if a user's input that I store as a string can be compared to an answer that I expect.

To sum: is there an equivalent syntax for if () for a string?

//Second task
        int age;
        int rep;


        Console.WriteLine ("How old are you? ");
        age = Convert.ToInt32 (Console.ReadLine ());
        Console.WriteLine ("What is the your age divided by your first number submitted in the previous question? ");
        rep = Convert.ToInt32 (Console.ReadLine ());
        if (rep == age / num01 ) {
            Console.WriteLine ("That is correct. Proceed to the next question. ");
        } else
        {
            Console.WriteLine ("That is incorrect. Start over. ");
        }
        Console.WriteLine ();

        //Third task
        string ans;

        Console.WriteLine ("Is your answer to the previous question an even or odd number? ");
        ans = Console.ReadLine ();
        if (rep % 2 == 0 && ans == even)
        {
            Console.WriteLine ("That is correct. ");
        }
        if (rep % 2 == 1 && ans == odd) 
        {
            Console.WriteLine ("That is correct. ");
        }   

Aucun commentaire:

Enregistrer un commentaire