Basic C# question regarding strings and if statements
Hello everyone.
I'm trying to make a console application in C#. It's just a very basic program. It asks the user to input some text, and it will give a response based on this. The program also has to make sure that Java, java, JAVA, jaVA, etc. are all the same. This is my code so far:
http://pastebin.me/490eb15ca27f1
I am not sure whether answer.Equals works for this case. The compiler is giving errors. It says it expects a ( and a ) somewhere around every 'answer' word. Any help will be greatly appreciated!
AlanPosted Nov 3, 2008, 4:58 AM
To deal with varying capitalization, I'd convert 'answer' to lower case and then just test the result against the lower case variants of the possible strings.
The other problem you have is that to test for equality you need to use the == operator. The = operator is used for assignment:
string answer;
string java;
java = "java";
Console.WriteLine("Java, C, Cobol or Python?");
answer = Console.ReadLine();
answer = answer.ToLower(); // now lower case
if (answer == java){
Console.WriteLine("Java text");
}
else if (answer == "c"){
Console.WriteLine("C text");
}
else if (answer == "cobol"){
Console.WriteLine("Cobol text");
}
else if (answer == "python"){
Console.WriteLine("Python text");
}
else Console.WriteLine("x");
Console.ReadLine();
nPosted Nov 3, 2008, 4:07 AM