Question Contains

Jconn250

New member
Joined
Mar 28, 2020
Messages
2
Programming Experience
Beginner
Hey there, new here, slowly learning the ins and outs of here and c#. I’m trying to understand the Contains command, but I don’t see what’s wrong with the following:
C#:
Console.WriteLine("What are the reactants required for the first step of the citric acid cycle?");
answer = Console.ReadLine();
if (answer.Contains("Oxaloacetate" + "Acetyl CoA"))
{
    Console.WriteLine("Correct");
}
else
{
    Console.WriteLine("Incorrect");
}
It returns incorrect when I type in the correct strings
 
Last edited by a moderator:
This:
C#:
if (answer.Contains("Oxaloacetate" + "Acetyl CoA"))
is the same as this:
C#:
if (answer.Contains("OxaloacetateAcetyl CoA"))
Now do you see what the issue is? You need to understand Boolean logic a bit better. If you want to look for two values then you look for the first value and then you look for the second value. You don't combine the values and look for the result. That code should be this:
C#:
if (answer.Contains("Oxaloacetate") && answer.Contains("Acetyl CoA"))
That's still not necessarily ideal because it doesn't preclude all sorts of other rubbish in the answer and it is also case-sensitive. It's a step in the right direction though.
 
Thanks so much! I know I’ve got a lot to learn, facing down a textbook is daunting so I like to take some time to figure out a little project and do it when I get bored. So yeah, I definitely need to read up on Boolean logic. Thanks again
 
Back
Top Bottom