【问题标题】:Creating a Random number(in Class) for later extensive use in program C#创建一个随机数(在类中)以供以后在程序 C# 中广泛使用
【发布时间】: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,并确保描述您遇到的实际问题。

标签: c# class random numbers


【解决方案1】:
  • 应在函数外部声明随机数。
  • 种子应该是有效的。
  • 如果您想要一个介于 1 和 6 之间的数字(包括),您应该在 Next() 函数中输入从 1 到 7 的数字(因为不包括 7)

所以,您的代码应该如下所示:

class Tamagochi
{

    private int ranNum1;
    private int ranNum2;
    private int ranNum3;
    private int ranNum4;
    Random RandomNumberGenerator;

    public Tamagochi()
    {
        RandomNumberGenerator = new Random(Guid.NewGuid().GetHashCode());
    }
    public void GetRandomNumber()
    {
        ranNum1 = RandomNumberGenerator.Next(1,7);
        ranNum2 = RandomNumberGenerator.Next(1,7);
        ranNum3 = RandomNumberGenerator.Next(1,7);
        ranNum4 = RandomNumberGenerator.Next(1,7);
    }
}

【讨论】:

  • 谢谢,我现在完全改变了它,以便可以在程序中生成随机数并且我让它工作。 code tamagochi1.DecreaseBoredomLvl(ranNum1); tamagochi1.IncreaseHungerLvl(ranNum2); tamagochi2.IncreaseBoredomLvl(ranNum3); tamagochi2.IncreaseHungerLvl(ranNum4); 然后我在类中使用“随机”作为参数对其进行了编辑但是,感谢您的帮助,我相信我很快就会使用它。
猜你喜欢
  • 2012-02-04
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 2014-10-03
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多