C # - Replacing an item in a backpack

I am trying to create a simple backpack program. This is a school assignment, and I almost finished, except that I cannot replace the item in my backpack with a new one.

My guess why it might not work is that I cannot replace itemit because it is in a global area, but I have no idea how to fix it. Here is the code:

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

Any ideas on how to fix this? Tips are greatly appreciated! Thank!

+4
source share
1 answer

Move destination

string item;
item = "Empty space";

Before the while loop.

Right now, every time you loop, you overwrite the value of the element.

This is how the whole code looks after the change:

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;
            }
        }
    }
+3
source

Source: https://habr.com/ru/post/1669402/


All Articles