-2
我有這樣的應用程序,我寫在C#只是實踐的目的,這是代碼,C# - 循環讓我的應用程序不propely工作
using System;
namespace Stock_Control
{
class Program
{
static void Main(string[] args)
{
int qty, index;
string name, location, code;
Item item = new Item();
ConsoleKeyInfo opt;
do
{
Console.Clear();
Console.WriteLine();
Console.WriteLine("******************** STOCK CONTROL SOFTWARE *********************");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Chose a option:");
Console.WriteLine("1 - Add item.");
Console.WriteLine("2 - View Stock.");
Console.WriteLine("3 - Remove item.");
Console.WriteLine("4 - Press 'Escape' to exit.");
opt = Console.ReadKey(true);
switch (opt.KeyChar.ToString())
{
case "1":
Console.WriteLine();
Console.Write("Enter the item code:");
code = Console.ReadLine();
Console.Write("Enter the item quantity:");
qty = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the item name:");
name = Console.ReadLine();
Console.Write("Enter the item location:");
location = Console.ReadLine();
item.add(code, qty, name, location);
break;
case "2":
Console.WriteLine("teste");
//item.view();
break;
case "3":
Console.WriteLine();
Console.WriteLine("Enter the index of the item to be removed:");
index = Int32.Parse(Console.ReadLine());
item.remove(index);
break;
}
} while (opt.Key != ConsoleKey.Escape);
}
}
}
它工作正常,但由於某些原因,當我包裹在一個循環中,switch語句中的第二個case停止工作,一旦我刪除循環,一切正常。我無法弄清楚什麼是錯的。
有人可以幫我嗎?
謝謝。
你能提供有問題的代碼,而不是糊紙盒。 –
把代碼放在這裏 – JerryGoyal
你只讀一個鍵:opt = Console.ReadKey(true);. opt也包含回報。所以第二次通過循環你正在閱讀回報,而不是鍵入的鍵。 – jdweng