【发布时间】:2017-04-19 16:29:54
【问题描述】:
我必须创建一个程序,其中玩家有 6 种选择,在 2 只宠物之间,他必须决定与每只宠物喂食、交谈或玩耍,但他一次只能做一个并且每次他选择一个选项时,所选择的选项(谈话时除外)会使其他选项增加而前一个选项减少。
我在各个方面都遇到了麻烦,无论是在程序内部还是在课堂上。 我似乎无法在 Class 中定义随机数,这导致程序无法运行。
到目前为止,这是 Class 的代码,这里肯定是问题所在:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tamagochi
{
class Tamagochi
{
private string tamagochi;
private double hungerLvl;
private double boredomLvl;
private double totalLevels;
private static int time;
private static int RandomNumberGenerator;
private static int ranNum1;
private static int ranNum2;
private static int ranNum3;
private static int ranNum4;
public Tamagochi (string tamagochi)
{
this.tamagochi = tamagochi;
}
public Tamagochi (string tamagochi, double hungerLvl, double boredomLvl, double randomNumbers, int RandomNumberGenerator)
{
this.tamagochi = tamagochi;
this.hungerLvl = hungerLvl;
this.boredomLvl = boredomLvl;
this.RandomNumberGenerator = RandomNumberGenerator;
}
public static void IncreaseTime()
{
++time;
}
public static void IncreaseBoredomLvl(double boredomLvl, int random)
{
boredomLvl += // randomnumber
}
public static void IncreaseHungerLvl(double hungerLvl)
{
hungerLvl += //randomnumber
}
public static void randomNumber(int min, int max)
{
Random RandomNumberGenerator = new Random();
ranNum1 = RandomNumberGenerator.Next(1, 6);
ranNum2 = RandomNumberGenerator.Next(1, 6);
ranNum3 = RandomNumberGenerator.Next(1, 6);
ranNum4 = RandomNumberGenerator.Next(1, 6);
}
//return section
public string GetTamagochi()
{
return tamagochi;
}
public double GetBoredomLvl()
{
return boredomLvl;
}
public double GetHungerLvl()
{
return hungerLvl;
}
public static int GetTime()
{
return time;
}
public static int GetRandomNum()
{
return ranNum1;
}
}
}
对尝试完成这项工作有任何帮助吗?
【问题讨论】:
-
不要在方法
randomNumber中创建Random实例,而是通过方法参数传递它或使用类中的字段。否则,如果您在循环中调用此方法,很可能会得到相同的数字,因为Random-constructor 使用当前时间作为种子。 -
请将其简化为minimal reproducible example,并确保描述您遇到的实际问题。