![](/img/trans.png)
[英]Is there a way to disallow clicking outside of winforms (c#) until user presses close button?
[英]Execute a loop until user presses a specific button
我正在嘗試創建一個多功能程序。 一方面,一個數組填充有隨機數。 然后,用戶輸入一個數字,程序返回該數字出現在數組中的哪個位置。 它還返回該數字出現在數組中的次數。
但是,它僅執行一次,然后結束程序。 我希望它提示用戶輸入要搜索的數字,直到用戶按下按鈕為止,例如說“ P”。 結果顯示后,一旦用戶按“ P”,程序應關閉。 關於應使用哪種方法或功能的任何提示?
這是我的代碼的細分版本。
Console.Write("Now enter a number to compare: ");
int c = Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < arr.Length; j++)
{
if (arr[j] == c)
{
pos.Add(j);
}
}
if (pos.Count == 0)
{
Console.WriteLine("Sorry this number does not match");
}
else
{
Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
}
Console.ReadLine();
這應該給您一些入門
您必須在代碼周圍使用循環並檢查要退出的關鍵字
class Program
{
static void Main(string[] args)
{
var arr = new int[50];
var pos = new List<int>();
string result;
do
{
Console.Write("Now enter a number to compare: ");
result = Console.ReadLine();
int c;
if (int.TryParse(result, out c))
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[j] == c)
{
pos.Add(j);
}
}
if (pos.Count == 0)
{
Console.WriteLine("Sorry this number does not match");
}
else
{
Console.WriteLine("The number {0} appears {1} time(s)", c, pos.Count);
}
}
} while (result != "exit");
}
}
我將提供另一種方法,但是沒有測試下面的代碼。
class Program
{
//declare your class variables here
//so that you can access them from the methods and do your operations
bool Up=true;
static void Main(string[] args)
{
Console.WriteLine("Program started.");
ThreadPool.QueueUserWorkItem(ConsoleCommands);
while (Up)
{
Thread.Sleep(2000);
}
Console.WriteLine("Program ended.");
}
private static void ConsoleCommands(object dummy)
{
while (Up)
{
string cmd = ConsoleReceiver().ToLower();
switch (cmd)
{
case "exit":
Up=false;
break;
//implement more cases here and fill the rest of your business
//example:
case "1":
if (pos.Count == Int32.Parse(cmd))//just a dummy business
{
Console.WriteLine("Sorry this number does not match");
}
else//another dummy business
{
Console.WriteLine("Sth...");
}
break;
default:
Console.WriteLine("Unrecognized command");
break;
}//or forget about switch and use if-else stements instead.
}
}
private static string ConsoleReceiver()
{
Console.WriteLine("#cmd:");
return Console.ReadLine();
}
}
如果要顯式讀取單鍵筆觸...
ConsoleKeyInfo keyInfo;
do {
Console.Write("Enter a number to compare; press the 'p' key to quit: ");
keyInfo = Console.ReadKey(false);
int c;
if (Int32.TryParse(keyInfo.KeyChar.ToString(), out c))
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[j] == c)
{
pos.Add(j);
}
}
if (pos.Count == 0)
{
Console.WriteLine("Sorry this number does not match");
}
else
{
Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
}
} while (keyInfo.Key != ConsoleKey.P)
否則,您可以結合@Fredou和我發布的內容來發揮創意。
嘗試這個 :)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
start:
string tryagain;
//All of your Code Goes here
tryagain = Console.ReadLine();
if (tryagain != "p")
{
goto start;
}
else
{
Environment.Exit(0);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.