C# C语言中的变量#

C# C语言中的变量#,c#,variables,C#,Variables,我一直在寻找解决这一小部分代码的答案,但运气不好。我想我必须在方法之外声明“item”的变量,但我似乎找不到这样做的方法 欢迎来到堆栈溢出!这里不太清楚您在问什么-问题通常应该包括您期望代码做什么,以及它实际做什么。也就是说,我想我知道你想要什么。我已经更新了代码,并添加了一些注释来描述更改 using System; using System.Collections.Generic; // This namespace contains the 'List<>' class n

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


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

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;
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;//此命名空间包含“List”类
命名空间项
{
班级计划
{
静态void Main(字符串[]参数)
{
//我已经把它移到了循环之外——我想用户会想要帮助消息
//在启动时显示,而不是每次他们做某事时显示。
Console.WriteLine(“欢迎”);
Console.WriteLine(“[1]-添加项”);
Console.WriteLine(“[2]-参见内容”);
Console.WriteLine(“[3]-擦除内容”);
控制台写入线(“[4]-关闭”);
//项目列表在这里创建,在主循环之外。
var items=新列表();
//您不需要在此处声明变量“myBool”
//我们可以循环直到用户退出整个程序
while(true)
{
int input=Convert.ToInt32(Console.ReadLine());
开关(输入)
{
案例1:
Console.WriteLine(“添加项”);
var newItem=Console.ReadLine();
WriteLine(“您刚才添加了:“+newItem”);
打破
案例2:
Console.WriteLine(“添加的项目:”);
foreach(项目中的var项目)
控制台写入线(项目);
打破
案例3:
items.Clear();
打破
案例4:
返回;
}
}
}
}
}

我建议您先学习一些教程,这样您就可以了解变量的作用域—变量的使用期限以及可以从何处访问它。

这取决于您在这里尝试执行的操作。在第33行,您想显示所有项目,还是只显示要添加的上一个项目

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

static void Main(字符串[]args)
{
bool myBool=true;
var项目;
while(myBool)
{
Console.WriteLine(“欢迎”);
Console.WriteLine(“[1]-添加项”);
Console.WriteLine(“[2]-参见内容”);
Console.WriteLine(“[3]-擦除内容”);
控制台写入线(“[4]-关闭”);
int input=Convert.ToInt32(Console.ReadLine());
开关(输入)
{
案例1:
Console.WriteLine(“添加项”);
item=(Console.ReadLine());
WriteLine(“您刚才添加了:“+item”);
打破
案例2:
Console.WriteLine(“添加的项目:+项目”);
打破
案例3:
myBool=假;
打破

}
为什么不把它放在
myBool
?将其清空并填入循环中。请注意,在您展示的代码中,真正的方法并不是“方法”。您的问题中只有一种方法,因此“不同方法”的含义还不是100%清楚。也就是说,很可能您在变量作用域方面遇到了问题。请参阅duplicate。非常感谢您的响应!很抱歉我不清楚我对代码的意图,我不需要项目列表,我只希望能够调用用户在案例1到案例2中写下的项目。非常感谢您的响应!很抱歉没有清楚我对代码的意图。我只想将案例1中的输入用于案例2。当我运行此代码时,“var item”在switch语句之外,我得到一个错误:“隐式类型的变量必须初始化”。将声明从
var-input;
更改为
var-input=string.Empty;
string-input=null;
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;
                }
            }
        }
    }
}