C# - 更换背包中的物品

时间:2017-02-10 17:28:12

标签: c#

我试图创建一个简单的背包程序。这是学校的一项任务,我几乎已经完成了,除非我无法用新的替换背包中的物品。

我猜测它可能不起作用的原因是我无法替换item,因为它在全局范围内,但我不知道如何修复它。这是代码:

static void Main(string[] args)
    {
        bool isRunning = true;
        while (isRunning)
        {
            Console.WriteLine("\n\tWelcome to the backpack!");
            Console.WriteLine("\t[1]Add an item");
            Console.WriteLine("\t[2]Show contents");
            Console.WriteLine("\t[3]Clear contents");
            Console.WriteLine("\t[4]Exit");
            Console.Write("\tChoose: ");

            int menyVal = Convert.ToInt32(Console.ReadLine());

            string item;
            item = "Empty space";


            switch (menyVal)
            {
                case 1:
                    Console.WriteLine("\n\tContents of backpack:");
                    Console.WriteLine("\n\t" + item);
                    Console.WriteLine("\n\tWhat do you want to replace " + item + " with?");
                    item = item.Replace(item, Console.ReadLine()); 
                    Console.WriteLine("\n\tYou have packed " + item + " in your backpack");
                    break;
                case 2:
                    Console.WriteLine("\n\tContents of backpack:");
                    Console.WriteLine("\n\t" + item);
                    Console.WriteLine("\n\tPress any key...");
                    Console.ReadKey();
                    break;
                case 3:
                    item = "Tom plats";
                    Console.WriteLine("\n\tYou have emptied the backpack!");
                    break;
                case 4:
                    isRunning = false;
                    break;
                default:
                    Console.WriteLine("Incorrect input!");
                    break;
            }
        }
    }

有关如何解决此问题的任何想法?提示非常感谢! 谢谢!

1 个答案:

答案 0 :(得分:3)

移动

的分配
string item;
item = "Empty space";

在while循环之前。

现在,每次循环都会覆盖项目值。

以下是整个代码在更改后的样子:

static void Main(string[] args)
    {
        bool isRunning = true;
        string item = "Empty space";

        while (isRunning)
        {
            Console.WriteLine("\n\tWelcome to the backpack!");
            Console.WriteLine("\t[1]Add an item");
            Console.WriteLine("\t[2]Show contents");
            Console.WriteLine("\t[3]Clear contents");
            Console.WriteLine("\t[4]Exit");
            Console.Write("\tChoose: ");

            int menyVal = Convert.ToInt32(Console.ReadLine());

            switch (menyVal)
            {
                case 1:
                    Console.WriteLine("\n\tContents of backpack:");
                    Console.WriteLine("\n\t" + item);
                    Console.WriteLine("\n\tWhat do you want to replace " + item + " with?");
                    item = Console.ReadLine());
                    Console.WriteLine("\n\tYou have packed " + item + " in your backpack");
                    break;
                case 2:
                    Console.WriteLine("\n\tContents of backpack:");
                    Console.WriteLine("\n\t" + item);
                    Console.WriteLine("\n\tPress any key...");
                    Console.ReadKey();
                    break;
                case 3:
                    item = "Tom plats";
                    Console.WriteLine("\n\tYou have emptied the backpack!");
                    break;
                case 4:
                    isRunning = false;
                    break;
                default:
                    Console.WriteLine("Incorrect input!");
                    break;
            }
        }
    }