在访问交换机中返回的字符串数组时遇到问题

问题描述 投票:0回答:1

[目前正在做一个学校项目,我似乎无法为这里发生的事情所困扰。我想做的是创建一个可以添加项目,删除项目等的图书馆系统,目前正在使用Additem方法,并且我已将所需的3个变量放入字符串中,并将该字符串返回给主菜单的开关。希望它将信息保留在内存中。但是当它返回到交换机时,当我尝试写行以验证信息是否进行了传输时,我只打印了“ system.string []”,而不是字符串的3个变量。。这是代码的两个部分,首先是数组

    public Array AddItem()
        {
            string Input = "";
            while (true)
            {
                //Media type selection screen
                Console.WriteLine("Please select the Item type");
                Console.WriteLine("1. Book");
                Console.WriteLine("2. DVD");
                Console.WriteLine("3. CD");
                Console.WriteLine("4. Magazines");
                Console.WriteLine("0. Back");
                Console.Write(":>");
                Input = Console.ReadLine();
                if (Input == "1" && Input.Length < 2)
                {
                    //Book information requests
                    string[] Bookinfo = new string[3];
                    Console.Write("What is the title of the Item: ");
                    Bookinfo[0] = Console.ReadLine();
                    Console.Write("What is the Value of the Item: ");
                    Bookinfo[1] = Console.ReadLine();
                    Console.Write("What is the ISBN Of the Item: ");
                    Bookinfo[2] = Console.ReadLine();
                    Console.WriteLine($"Item {Bookinfo[0]} with the value {Bookinfo[1]} has been added to the system");
                    System.Threading.Thread.Sleep(700);
                    return Bookinfo;
                }

和现在的开关

                        case 2:
                            {
                                if (Input == "1")
                                {
                                 Array ItemInfo = Library.AddItem();
                                    Console.WriteLine($"{ItemInfo}");
                                    Console.ReadKey();
                                }
                                break;
                            }

非常感谢您的帮助。

c#
1个回答
1
投票

您的问题是这一行:Console.WriteLine($"{ItemInfo}");

[ItemInfoArray,它没有实现ToString方法,因此Console.WriteLine将打印其类型。

要打印3个变量,您可以像这样使用String.Join

Console.WriteLine(String.Join(", ", (string[])ItemInfo));

此外,您不必根据发送的内容使用Array,只需使用string[]

不相关,当您仅打印一个对象时,可以使用Console.WriteLine(Item);代替Console.WriteLine($"{Item}");,该字符串是不必要的。

© www.soinside.com 2019 - 2024. All rights reserved.