【问题标题】:C# - Replacing an item in the backpackC# - 替换背包中的物品
【发布时间】:2017-02-10 17:28:12
【问题描述】:

我正在尝试创建一个简单的背包程序。这是学校的作业,我几乎完成了,只是我无法用新背包替换背包中的物品。

我猜测为什么它可能不起作用是我无法替换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 时,系统会提示用户输入应该替换存储在“item”中的字符串的内容。但它并没有,它与第一个声明“空白空间”保持相同。
  • 由于item 只是一个字符串,您可以简单地分配它item = Console.ReadLine(); 而不是使用item.Replace();
  • 请阅读How to debug

标签: c#


【解决方案1】:

移动赋值

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;
            }
        }
    }

【讨论】:

  • 非常感谢!我知道这将是一件我忽略的简单事情。完全忘记了我是嵌套在一个while循环中的。 *doh
  • item = item.Replace(item, Console.ReadLine()); 仍然是不必要的;只需分配item = Console.ReadLine());
  • 我使用item = item.Replace(item, Console.ReadLine()); 的原因是因为我在另一篇文章中读到它可以解决我的问题。没那么明显我要回item = Console.ReadLine());
  • 我会的,你的系统太快了,所以我得等一会儿。 x)
猜你喜欢
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多