Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能';无法获取库存();运行_C# - Fatal编程技术网

C# 我能';无法获取库存();运行

C# 我能';无法获取库存();运行,c#,C#,我是c#的新手,我已经学习了语言基础知识,我正在尝试制作一个基于文本的rpg,在这里输入命令,然后它执行命令。 我现在已经把它,以便我可以打开库存,但库存功能将不会运行,我不知道为什么。 它没有给出错误。对不起,代码太乱了。我已经用注释标记了函数。希望能有帮助 //lists using System.Collections.Generic; //dont know how to rename file namespace Security_program {//create an rpg c

我是c#的新手,我已经学习了语言基础知识,我正在尝试制作一个基于文本的rpg,在这里输入命令,然后它执行命令。 我现在已经把它,以便我可以打开库存,但库存功能将不会运行,我不知道为什么。 它没有给出错误。对不起,代码太乱了。我已经用注释标记了函数。希望能有帮助

//lists
using System.Collections.Generic;

//dont know how to rename file
namespace Security_program
{//create an rpg class which most of my functions is
    class RPG
    {
        public List<string> _inventory = new List<string>();
        public List<string> usable_items = new List<string>();
        public void set_items()
        {
            usable_items.Add("Health potion");
        }
//\/this is the part i need to run \/sorry for messy code
        public void inventory()
        {
            bool in_inventory = true;
            while(in_inventory == true)
            {
                for (int i = 0; i < _inventory.Count; i++)
                {
                    if(_inventory.Count == i)
                    {
                        Console.WriteLine("You have nothing in your inventory!");
                    }
                    else
                    {
                    Console.WriteLine( i+1 +". "+_inventory[i]);
                    }
                }

            }
        }
        //uses command input funcction and knows what to do withn it



        public void action(string action)
        {
            if(action == "i")
            {
                inventory();
            }
        }//takes command inputs
        public string command_check(string command)
        {//quit in case they dont want to play anymore(impossible but ok i guess)
            if(command=="!quit")
            {
                string t = "q";
                return t;
            }//if they dont want to quit they can do stuff
            else if(command == "!inventory")
            {
                string t = "i";
                return t;
            }
            else
            {
                string t = "n";
                Console.WriteLine("Makea decision!");
                return t;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            RPG rpg = new RPG();
            rpg.set_items();
            bool GameEnd = false;
            while (GameEnd == false)
            {
                string Text = Console.ReadLine();
                string action = rpg.command_check(Text);
                if(action == "q")
                {
                    return;
                }
                else
                {
                    rpg.action(action);
                }
                
            }
            Console.WriteLine("You died!");
            Console.ReadKey();
            
        }
    }
}```
//列表
使用System.Collections.Generic;
//不知道如何重命名文件
名称空间安全程序
{//创建一个rpg类,我的大多数函数都是
类RPG
{
公共列表_inventory=新列表();
公共列表可用_项=新列表();
公共无效集合_项()
{
可用物品。添加(“健康药水”);
}
//\/这是我需要运行的部分\/对不起,代码太乱了
公开作废清单()
{
库存中的布尔=真;
while(in_inventory==true)
{
对于(int i=0;i<\u inventory.Count;i++)
{
如果(_inventory.Count==i)
{
WriteLine(“您的库存中没有任何内容!”);
}
其他的
{
Console.WriteLine(i+1++“++_inventory[i]);
}
}
}
}
//使用命令输入函数并知道如何使用它
公共作废操作(字符串操作)
{
如果(动作=“i”)
{
存货();
}
}//接受命令输入
公共字符串命令\u检查(字符串命令)
{//如果他们不想再玩了,就退出(不可能,但我想可以)
如果(命令==“!退出”)
{
字符串t=“q”;
返回t;
}//如果他们不想退出,他们可以做一些事情
else if(命令==“!库存”)
{
字符串t=“i”;
返回t;
}
其他的
{
字符串t=“n”;
Console.WriteLine(“做决定!”);
返回t;
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
RPG RPG=新RPG();
rpg.set_items();
bool GameEnd=false;
while(GameEnd==false)
{
string Text=Console.ReadLine();
string action=rpg.command\u check(文本);
如果(动作==“q”)
{
返回;
}
其他的
{
rpg.行动(行动);
}
}
Console.WriteLine(“你死了!”);
Console.ReadKey();
}
}
}```

已做了两项更改,请参阅注释以获取解释

public void inventory()
            {
                bool in_inventory = true;
                while(in_inventory == true)
                {
                   /* The loop termination condition is till i < _inventory.Count, so your if 
                        condition ie. _inventory.Count == i will never be true. 
                        So change your loop condition to i <= _inventory.Count
                    */
                    for (int i = 0; i <= _inventory.Count; i++)
                    {
                        if(_inventory.Count == i)
                        {
                            /*
                                Set value in_inventory = false; here as all _inventory items are visited.
                                Possibly you can remove the while loop, as the for loop iterates through all _inventory items.
                            */
                            in_inventory = false;
                            Console.WriteLine("You have nothing in your inventory!");
                        }
                        else
                        {
                        Console.WriteLine( i+1 +". "+_inventory[i]);
                        }
                    }
    
                }
            }
public void inventory()
{
库存中的布尔=真;
while(in_inventory==true)
{
/*循环终止条件为直到i<\u inventory.Count,因此如果
条件ie.\u inventory.Count==我永远不会为真。

因此,将循环条件更改为i它确实运行,您只是从不在
\u inventory
中放入任何内容,这样for循环就不会运行(如果
如果
语句没有真正意义,如果
\u inventory.Count==0
,那么
i<\u inventoryCount
就永远不会
为true
)您需要检查for循环之外的空库存,我严重怀疑您是否想要无限while循环。此外,您没有在任何地方更改\u inventory
中的
值。您的
while
循环是无限的。
inventory()
在键入“!inventory”时运行根据你的代码,你将运行到一个无休止的循环中,因为你不会在一段时间内将库存中的
变成false