简体   繁体   中英

c# Trying to loop through a string array , compare a string to find, and output the found string

A program to find a book in an array of books. This code doesn't find the book?

string aBookToFind;
string[] listOfBooks = { "Jane Eyre", "Catch22", "Dunkirk","Kidnapped", "Treasure Island" };
aBookToFind=Console.ReadLine();
for(int i=0;i<5;i++){
Console.WriteLine("Book : "+listOfBooks[i]);
if (aBookToFind == listOfBooks[i])
{Console.WriteLine("Found Book "+ listOfBooks[i];
}
}

There is a missing parentheses at line 7 before the ";"{Console.WriteLine("Found Book "+ listOfBooks[i]; After fixing this and running the code with the Input: Catch22 The Output was:

Book : Jane Eyre
Book : Catch22
Found Book Catch22
Book : Dunkirk
Book : Kidnapped
Book : Treasure Island

So I'd say other than the missing ")" it's working as expected. If you expected another output, maybe add it to your question for a more precise answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM