简体   繁体   English

c# 尝试遍历字符串数组,比较要查找的字符串,output 找到的字符串

[英]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 ";"第 7 行“;”之前缺少括号{Console.WriteLine("Found Book "+ listOfBooks[i]; After fixing this and running the code with the Input: Catch22 The Output was: {Console.WriteLine("Found Book "+ listOfBooks[i];修复此问题并使用输入运行代码后: Catch22 The Output 为:

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.如果您期待另一个 output,可以将其添加到您的问题以获得更准确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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