我正在尝试模拟经济,所以我需要的是我的原材料以稳定的速度堆积。我不确定如何继续,因为无论何时我使用+ =运算符,整数矿物都不会堆叠。
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“矿物”不会叠加。随机int“a”被添加到“矿物”中,但每次调用该类时,由于+ =“a”操作而不是int矿物质变大,它才会开始回到0。
如何告诉程序保持其价值?我需要一个不同的运营商吗?我需要不同的数据类型吗?
答案 0 :(得分:3)
当然,它会再次从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;
请注意,每次运行StarSystem
方法时,不都会创建Market
的新实例,您需要保留对您创建的实例的引用,以便您可以更新它们。
如果你有一堆这些(我假设你可能会这样做),那么你可以将它们放在一个集合中。字典可能很合适:
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
也应该是班级成员。从你的描述中我们并不清楚。