在 vscode 中使用 C# 输入控制台应用程序

发布于 2025-01-15 02:42:03 字数 923 浏览 4 评论 0原文

我试图通过制作一个控制台游戏来测试我的傻瓜大脑,因为我认为我很擅长编程,并且陷入了库存(即在库存中的每个项目上进行 foreach你按“我”)但意识到我太笨了。这就是我写的:

string i;
var input = Console.ReadKey();
if (input = 'I') {
    foreach (KeyValuePair<string, string> item in Inventory)
    {
        i += $"{item.Key}: {item.Value}";
    }
}
else {
    Console.WriteLine("(Press 'I' to open your inventory.)");
}

我知道,这可能会杀死一个程序员(我很无趣)。这是错误消息:

    C:\Documents\new pogram\internet be broke 2 electric boogaloo.cs(19,13): error CS0103: The name 'I' does not exist in the current context 
    
    C:\Documents\new pogram\internet be broke 2 electric boogaloo.cs(22,9): error CS0165: Use of unassigned local variable 'i' [C:\Users\lewis_pulfff2\OneDrive\Documents\new pogram\new pogram.csproj]

The build failed. Fix the build errors and run again.

我尝试寻找其他软件,但示例不起作用。

arigatogozaimasu(和 gomennasorry)提前:D。

i tried to test my dum-dum brain by making a console game because i thought that i was gud at programming, and got stuck on the inventory (i.e. going through a foreach for each item in the inventory when you press 'I') but realised i'm too dum-dum for that. here's what i wrote:

string i;
var input = Console.ReadKey();
if (input = 'I') {
    foreach (KeyValuePair<string, string> item in Inventory)
    {
        i += 
quot;{item.Key}: {item.Value}";
    }
}
else {
    Console.WriteLine("(Press 'I' to open your inventory.)");
}

i know, this could kill a PROgrammer (i'm so unfunny). here were the error messages:

    C:\Documents\new pogram\internet be broke 2 electric boogaloo.cs(19,13): error CS0103: The name 'I' does not exist in the current context 
    
    C:\Documents\new pogram\internet be broke 2 electric boogaloo.cs(22,9): error CS0165: Use of unassigned local variable 'i' [C:\Users\lewis_pulfff2\OneDrive\Documents\new pogram\new pogram.csproj]

The build failed. Fix the build errors and run again.

i tried looking else ware, but the samples didn't work.

arigatogozaimasu (and gomennasorry) in advance :D.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-01-22 02:42:03

我假设您曾经

   if (input = I) 

在发布代码之前更改过它 - 这是第一个错误的原因。

您还意味着

   if (input == 'I')  <<<  '==' means compare '=' means assign

EDIT

加 ReadKey 返回的内容多于按键 - 请参阅 https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-6.0

所以你需要

  if(input.Key == 'I')

第二个错误来自

 string i;   <<<<======= this doesnt create an empty string (i is null)

你需要的

string i = "";
var input = Console.ReadKey();
if (input == 'I') {
    foreach (KeyValuePair<string, string> item in Inventory)
    {
        i += $"{item.Key}: {item.Value}";
    }
}
else {
    Console.WriteLine("(Press 'I' to open your inventory.)");
}

I will assume that you used to have

   if (input = I) 

but changed that before posting the code - this was the cause of the first error.

also you mean

   if (input == 'I')  <<<  '==' means compare '=' means assign

EDIT

plus ReadKey returns more than the key press - see https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-6.0

so you need

  if(input.Key == 'I')

THe second error comes from here

 string i;   <<<<======= this doesnt create an empty string (i is null)

you need

string i = "";
var input = Console.ReadKey();
if (input == 'I') {
    foreach (KeyValuePair<string, string> item in Inventory)
    {
        i += 
quot;{item.Key}: {item.Value}";
    }
}
else {
    Console.WriteLine("(Press 'I' to open your inventory.)");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文