【问题标题】:Variables in C# [duplicate]C#中的变量[重复]
【发布时间】:2020-11-15 21:06:15
【问题描述】:

我一直在寻找解决这一点代码的答案,但没有运气。我想我必须在方法之外为“item”声明变量,但我似乎找不到这样做的方法。

beginner

namespace Items
{
    class Program
    {
        static void Main(string[] args)
        {
            bool myBool = true;

            while (myBool)
            {
                Console.WriteLine("Welcome");
                Console.WriteLine("[1] - add item");
                Console.WriteLine("[2] - see content");
                Console.WriteLine("[3] - erase content");
                Console.WriteLine("[4] - close");

                int input = Convert.ToInt32(Console.ReadLine());
        
                switch (input)
                {
                    case 1:
                        Console.WriteLine("add item");

                        var item = (Console.ReadLine());

                        Console.WriteLine("you just added: " + item);
                        break;

                    case 2: 
                        Console.WriteLine("items added: " + items);
                        break;

                    case 3:  
                        myBool = false;
                        break;
                }
            }
        }
    }
}

【问题讨论】:

  • 为什么不把它放在myBool 上呢?留空并将其填充到循环中。请注意,您显示的代码中的真正不是“方法”。
  • 您的问题中只有一种方法,因此“不同方法”的含义并不是 100% 清楚。也就是说,您很可能在变量范围界定方面遇到了问题。查看副本。

标签: c# variables


【解决方案1】:

欢迎来到堆栈溢出!您在这里问的不是很清楚 - 问题通常应该包括您期望代码做什么,以及它实际做什么。也就是说,我想我可以看到你想要什么。我更新了代码,并添加了一些 cmets 来描述更改。

using System;
using System.Collections.Generic; // This namespace contains the 'List<>' class

namespace Items
{
    class Program
    {
        static void Main(string[] args)
        {
            // I've moved this outside of the loop - I assume users would want the help message
            // displayed on startup, not every single time they do something.
            Console.WriteLine("Welcome");
            Console.WriteLine("[1] - add item");
            Console.WriteLine("[2] - see content");
            Console.WriteLine("[3] - erase content");
            Console.WriteLine("[4] - close");

            // The list of items is created here, outside of the main loop.
            var items = new List<string>();

            // You don't need to declare a variable 'myBool' here
            // We can just loop until the user exits the whole program
            while (true)
            {
                int input = Convert.ToInt32(Console.ReadLine());

                switch (input)
                {
                    case 1:
                        Console.WriteLine("add item");
                        var newItem = Console.ReadLine();
                        Console.WriteLine("you just added: " + newItem);
                        break;

                    case 2:
                        Console.WriteLine("items added:");
                        foreach (var item in items)
                            Console.WriteLine(item);

                    break;

                    case 3:
                        items.Clear();
                        break;

                    case 4:
                        return;
                }
            }
        }
    }
}

我建议先学习一些教程,这样您就可以了解变量范围 - 变量的生存时间以及可以从哪里访问它。

【讨论】:

  • 非常感谢您的回复!抱歉不清楚我对代码的意图,我不需要项目列表,我只是希望能够将用户在案例 1 中写下的项目调用到案例 2。
【解决方案2】:

这取决于您要在这里做什么。在第 33 行,您是要显示所有项目,还是只显示要添加的上一个项目?

您得到的错误是变量“items”不存在。如果只想显示上一个要添加的项目,则可以将“项目”一词替换为“项目”,并将声明“项目”的行移到 switch 语句之外,如下所示:

        static void Main(string[] args)
        {
        bool myBool = true;
        
        var item;

        while (myBool)

        {
            Console.WriteLine("Welcome");
            Console.WriteLine("[1] - add item");
            Console.WriteLine("[2] - see content");
            Console.WriteLine("[3] - erase content");
            Console.WriteLine("[4] - close");

            int input = Convert.ToInt32(Console.ReadLine());
        
        switch (input)
            {
                case 1:
                    Console.WriteLine("add item");

                    item = (Console.ReadLine());

                    Console.WriteLine("you just added: " + item);
                    break;

                case 2: 
                    Console.WriteLine("items added: " + item);

                    break;

                 case 3:  
                    myBool = false;

                    break;
            }

现在,我们在 Main 方法中而不是在 switch 语句中声明变量 item。以前,当您在 switch 语句中声明它时,每次重新启动循环时都会重置它。现在,当用户通过输入一个值来改变它时,它会在你回到循环开始时被保存,所以如果用户输入“2”,他们将看到他们输入的最后一个项目。

如果您希望向用户显示他们输入的每个项目,则需要使用数组或集合。

【讨论】:

  • 非常感谢您的回复!很抱歉不清楚我对代码的意图。我只想将案例 1 中的输入用于案例 2。当我运行此代码时,在 switch 语句之外使用“var item”,我收到错误:“必须初始化隐式类型变量”。
  • 将声明从 var input; 更改为 var input = string.Empty;string input = null;
猜你喜欢
  • 2012-04-12
  • 2013-06-09
  • 2021-08-30
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
相关资源
最近更新 更多