【发布时间】:2020-11-15 21:06:15
【问题描述】:
我一直在寻找解决这一点代码的答案,但没有运气。我想我必须在方法之外为“item”声明变量,但我似乎找不到这样做的方法。
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% 清楚。也就是说,您很可能在变量范围界定方面遇到了问题。查看副本。