![](/img/trans.png)
[英]c# WinForms - Keep a ContextMenu from closing after a click on certain Items
[英]C# how to keep searching for the items in my list instead of app just closing after one search
謝謝你的幫助,我是編程新手,我試過谷歌但找不到答案。
我正在尋找的是一種讓我繼續在我的列表中搜索項目而不會在僅一次搜索后關閉的方法。 太感謝了!
我試過的代碼是:
static void Main(string[] args)
{
string appName = "Fruitsalad Jens";
string appVersion = "1.0.0";
string appAuthor = "Jens Jonassen";
// Farge
Console.ForegroundColor = ConsoleColor.Green;
// Versjon
Console.WriteLine("{0} Versjon {1} by {2}", appName, appVersion, appAuthor);
// Tilbakestill Fargen
Console.ResetColor();
// Velkomst
Console.WriteLine("Hello, who are you?");
string navn = Console.ReadLine();
Console.WriteLine("Welcome to fruit finder {0}! Which fruit do you need to find?", navn);
{
string sSearch;
List<Frukter> listFrukter = new List<Frukter>();
listFrukter.Add(new Frukter());
listFrukter[0].Navn = "Orange";
listFrukter[0].Pris = 32.5;
listFrukter.Add(new Frukter());
listFrukter[1].Navn = "Apple";
listFrukter[1].Pris = 23;
listFrukter.Add(new Frukter());
listFrukter[2].Navn = "Grapes";
listFrukter[2].Pris = 18.90;
listFrukter.Add(new Frukter());
listFrukter[3].Navn = "Mango";
listFrukter[3].Pris = 48;
sSearch = Console.ReadLine();
for (int iCount = 0; iCount < listFrukter.Count; iCount++)
{
if (listFrukter[iCount].Navn.Equals(sSearch))
{
Console.WriteLine("Found it!");
Console.WriteLine(listFrukter[iCount].getData());
}
}
Console.ReadLine();
添加while
循環:當用戶想要繼續搜索時:
...
// keep on looping (infinite loop)...
while (true) {
Console.WriteLine("Welcome to fruit finder {0}! Which fruit do you need to find? Q for quit", navn);
sSearch = Console.ReadLine().Trim();
// ... until user enters "q" for quit
if (string.Equals(sSearch, "Q", StringComparison.OrdinalIgnoreCase))
break;
for (int iCount = 0; iCount < listFrukter.Count; iCount++)
{
if (listFrukter[iCount].Navn.Equals(sSearch))
{
Console.WriteLine("Found it!");
Console.WriteLine(listFrukter[iCount].getData());
}
}
Console.ReadLine();
}
...
編輯:不同實現中的相同想法(循環)(請參閱下面的 Pol Vilarrasa 評論):
// loop once and more if user wants to search again
do {
Console.WriteLine("Welcome to fruit finder {0}! Which fruit do you need to find?", navn);
sSearch = Console.ReadLine().Trim();
for (int iCount = 0; iCount < listFrukter.Count; iCount++)
{
if (listFrukter[iCount].Navn.Equals(sSearch))
{
Console.WriteLine("Found it!");
Console.WriteLine(listFrukter[iCount].getData());
}
}
Console.WriteLine("Another search Y/ N?");
sSearch = Console.ReadLine().Trim();
}
while (string.Equals(sSearch, "Y", StringComparison.OrdinalIgnoreCase));
...
我認為您正在尋找一個while 循環。 此構造采用 boolean 表達式,並在表達式返回true
時重復執行其主體中的代碼。
例如,以下代碼會將數字 0 到 9 打印到控制台:
int i = 0;
while(i < 10)
{
Console.WriteLine(i);
i = i + 1;
}
小心循環,因為如果條件永遠不會評估為false
,循環將永遠繼續。 在示例中,如果我們刪除i = i + 1;
,循環永遠不會終止,因為i
永遠是 0。
在您的情況下,您可能只想使用while(true)
,因為您想無限期地重復搜索。 從讀取用戶輸入到打印響應的所有內容都應該是while
主體的一部分。
我沒有任何示例代碼,但這是編程練習的典型示例。
如果我理解正確,您希望在第一次搜索后繼續搜索其他水果。
您需要添加一個循環,以便在完成搜索后返回以再次檢查輸入,如下所示:
sSearch = Console.ReadLine();
while (sSearch != "exit")
{
for (int iCount = 0; iCount < listFrukter.Count; iCount++)
{
if (listFrukter[iCount].Navn.Equals(sSearch))
{
Console.WriteLine("Found it!");
Console.WriteLine(listFrukter[iCount].getData());
}
}
sSearch = Console.ReadLine();
}
將 Console.Readline() 移動到 while 循環內。 一旦找到一個項目,就從內部 for 循環中中斷。
Console.WriteLine("Press ESC to stop");
while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
{
sSearch = Console.ReadLine();
for(int iCount = 0; iCount < listFrukter.Count; iCount++)
{
if (listFrukter[iCount].Navn.Equals(sSearch))
{
Console.WriteLine("Found it!");
Console.WriteLine(listFrukter[iCount].getData());
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.