【发布时间】:2017-06-23 06:45:27
【问题描述】:
我有这个应用程序,我用 C# 编写只是为了练习,这是代码,
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 语句中的第二种情况停止工作,一旦我删除循环,一切正常。我不知道出了什么问题。
有人可以帮我吗?
谢谢。
【问题讨论】:
-
您能否提供相关代码而不是粘贴箱。
-
把代码放在这里
-
您只读取一个键:opt = Console.ReadKey(true);。 opt 也包含返回。所以第二次通过循环你正在阅读返回而不是键入的键。
标签: c# loops switch-statement