【问题标题】:C# - loop makes my app not work propelyC# - 循环使我的应用程序无法正常工作
【发布时间】: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


【解决方案1】:

它仍然可以正常工作,但是当您添加循环时,您会执行 Console.WriteLine("teste"); item.view(); 以防万一“2”,然后立即重新开始 = 清除控制台,这样您就看不到任何东西。

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2015-06-08
    • 2016-05-09
    • 2019-01-01
    相关资源
    最近更新 更多