简体   繁体   English

代码不断吞噬内存

[英]Code keeps eating Memory

So basically this code asked how many items do I want to generate but after that just keep running without response. 因此,基本上,这段代码会询问我要生成多少个项目,但此后只要继续运行就不会产生响应。 And just eats up ram. 只是吃公羊。

What I want it to do is ask amount of items to generate then state its name and its stats. 我想要它做的是询问要生成的项目数量,然后说明其名称和统计信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingRNG
{
class Program
{
    public static Random Random = new Random();
    public static int dropRate, itemAmmount;
    public static List<Items> Loot = new List<Items>();
    public static List<string> WeaponList = new List<string> { "Staff", "Sword", "Dagger", "Wand", "Axe", "Mace" };
    static void Main(string[] args)
    {
        Programs();
    }
    public static void Programs()
    {
        Console.WriteLine("How many items do you want to generate");
        itemAmmount = Int32.Parse(Console.ReadLine());

        for (int i = 0; 0 < itemAmmount; i++)
        {
            dropRate = Random.Next(1, 6);
            Loot.Add(new Items(dropRate));
        }
        foreach (var Item in Loot)
        {
            Console.WriteLine(Item.Name);
            if (WeaponList.Contains(Item.ItemType))
            {
                Console.WriteLine(Item.Damage);
            }
            else
            {
                Console.WriteLine(Item.HP);
                Console.WriteLine(Item.MANA);
            }
            Console.ReadLine();

        }
    }
}

} }

for (int i = 0; 0 < itemAmmount; i++)

You're not checking the loop counter, you're comparing 0 with itemAmount . 您没有检查循环计数器,而是将0与itemAmount进行比较。 That's an infinite loop. 那是一个无限循环。

Try for (int i = 0; i < itemAmmount; i++) instead. 尝试for (int i = 0; i < itemAmmount; i++)

You can use code snippets in Visual Studio to avoid such errors. 您可以在Visual Studio中使用代码段来避免此类错误。 Type 'for' and press Tab twice, and a code snippet for for loop will be inserted with possibility to rename i and length : 输入'for'并按两次Tab键,将插入for循环的代码段,可以重命名ilength

for (int i = 0; i < length; i++)
{

}
for (int i = 0; 0 < itemAmmount; i++)
{
    dropRate = Random.Next(1, 6);
    Loot.Add(new Items(dropRate));
}

you set itemAmmount , and never alter it. 您设置itemAmmount ,并且永远不要更改它。 The only way your loop will exit is if itemAmmount decreased (to below 0). 循环退出的唯一方法是将itemAmmount减小(小于0)。

Fix your for loop, so that it makes logical sense: 修复您的for循环,使其具有逻辑意义:

for(int i=0; i<itemAmmount; i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM