整数值在调用方法时一直重置为零
本文关键字:一直 调用 方法 整数 | 更新日期: 2023-09-27 18:17:06
我正在尝试模拟经济,所以我需要的是我的原材料以稳定的速度堆积。我不确定如何继续,因为每当我使用+=操作符时,整数矿物不会堆叠。
namespace Finite_State_Machine_5
{
class Economy
{
public static void Market() // static void Main simply calls Economy.Market()
{
Console.WriteLine("This.");
Thread.Sleep(250);
int miningRate = 4; // This is the rate at which the resource is mined.
// If it is increased, Random() will be able to generate from a larger selection,
// increasing the chances of getting a larger integer.
int hydrogenIncome = RandomNumber.GetRandomClass(1, miningRate); // RandomNumber.GetRandomClass (omitted)
// generates a random number between 1 and miningRate
// hydrogenIncome is the integer which is continually increasing.
// Every time AlphaCygni.Income is called, it takes the hydrogenIncome integer and adds it to int mineral.
AlphaCygni.Income(hydrogenIncome);
ContinueLoop();
}
static void ContinueLoop()
{
Console.WriteLine("End.");
// ContinueLoop simply keeps the loop going, calling Economy.Market() so the whole process will continue.
Thread.Sleep(250);
Economy.Market();
}
}
}
namespace Finite_State_Machine_5
{
public class AlphaCygni : StarSystem
{
public static int Income(int a)
{
// Here with int mineral, you can see it starts at 0 but each addition with a (hydrogenIncome) should increase the number.
int mineral = 0;
Console.WriteLine(mineral);
return mineral;
// The result of the addition returns mineral
}
}
}
问题是int "mineral"不叠加。随机整型"a"被添加到"mineral"中,但每次调用该类时,int mineral不会因为+="a"操作而变得更大,而是开始返回0。
我如何告诉程序保持它的值?我需要另一个操作员吗?我需要不同的数据类型吗?
当然,它又从0
开始,这就是您在Income
方法中设置的。您需要mineral
为静态成员:
public class AlphaCygni : StarSystem
{
private static int mineral = 0;
public static int Income(int a)
{
// Here with int mineral, you can see it starts at 0 but each addition with a (hydrogenIncome) should increase the number.
mineral += a
Console.WriteLine(mineral);
return mineral;
// The result of the addition returns mineral
}
}
或者,你可以尝试理解面向对象编程,并使用类的实例,而不是一堆带有静态方法和成员的静态类。
不知道确切你在做什么。我将把它重构成这样:
public class StarSystem
{
public int Minerals { get; set; }
public string Name { get; private set; }
public StarSystem(string name)
{
Name = name;
}
}
现在,不必为每个系统创建派生类(这将很快变得难以管理),您只需创建StarSystem
的实例:
var alphaCygni = new StarSystem("Alpha Cygni");
增加Minerals
属性:
alphaCygni.Minerals += hydrogenIncome;
注意,每次运行Market
方法时,您将不创建StarSystem
的新实例,您需要保留对您创建的实例的引用,以便您可以更新它们。
如果你有一堆这样的元素(我猜你可能有),那么你可以把它们放在一个集合中。字典可能比较合适:
var starSystems = new Dictionary<string,StarSystem>();
starSystems["Alpha Cygni"] = new StarSystem("Alpha Cygni");
starSystems["Sol"] = new StarSystem("Sol");
//... and so on
当你想增加他们的Minerals
:
starSystems["Alpha Cygni"].Minerals += hydrogenIncome;
您甚至可以使用enum
而不是字符串作为键,以避免键入错误的系统名称。或者您可以创建从Dictionary
派生的自己的专用集合,以避免在创建新的StarSystem
并将其添加到集合中时必须键入两次名称。
或者如果您不需要通过名称快速访问StarSystem
的实例,一个简单的列表就足够了。
那么你的Economy
类可能变成这样:
public class Economy
{
private Dictionary<string,StarSystem> systems;
public Economy()
{
// Create and populate your systems
// We'll hardcode a couple here, but you might load them some external resource
systems = new Dictionary<string,StarSystem>();
starSystems["Alpha Cygni"] = new StarSystem("Alpha Cygni");
starSystems["Sol"] = new StarSystem("Sol");
}
public void Market()
{
Console.WriteLine("This.");
Thread.Sleep(250);
int miningRate = 4; // This is the rate at which the resource is mined.
// If it is increased, Random() will be able to generate from a larger selection,
// increasing the chances of getting a larger integer.
int hydrogenIncome = RandomNumber.GetRandomClass(1, miningRate); // RandomNumber.GetRandomClass (omitted)
// generates a random number between 1 and miningRate
// this will increase every system by the same amount
// that's probably not exactly what you want, but you can adapt as needed
foreach (var system in systems.Values)
{
system.Minerals += hydrogenIncome;
}
ContinueLoop();
}
private void ContinueLoop()
{
Console.WriteLine("End.");
// ContinueLoop simply keeps the loop going, calling Economy.Market() so the whole process will continue.
Thread.Sleep(250);
Market();
}
}
虽然我怀疑miningRate
和/或hydrogenEconomy
也应该是类成员。