在C#中获取骰子掷骰程序时出现问题

时间:2018-09-02 04:50:30

标签: c#

我在使用C#的程序时遇到问题,该程序需要掷骰子。在运行程序之前,指定模具上的边的数量,并根据面的数量显示随机值。这个主题相对较新,似乎无法正常使用。到目前为止,看起来像这样:

public class Die
{
    public int die_faces  = 6;
    public int face_value = 1;

    public Die(int faces)
    {
        int die_faces = faces;
        RollDie();
        GetFaceValue();
        GetNumFaces();
    }
    public void RollDie()
    {
        var rand = new Random();
        int face_value = rand.Next(die_faces);
    }
    public void GetFaceValue() =>  Console.WriteLine("current value of face: ", face_value);
    public void GetNumFaces()  => Console.WriteLine("current number of faces: ", die_faces);
}

public class Program
{
    public static void Main() => var myDie = new Die(7);
}

程序运行,不打印任何值。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

问题是您将值分配给新变量,并且在打印时使用了类变量。 因此,请更改以下两行。

  1. 在构造函数中,int die_faces = faces;die_faces = faces;
  2. 在RollDie()方法中,从int face_value = rand.Next(die_faces);face_value = rand.Next(die_faces);
  3. 需要修改带有@KozhevnikovDmitry所提到的参数索引的打印

    Console.WriteLine("current value of face: {0}", face_value);

    Console.WriteLine("current number of faces: {0}", die_faces);

如果使用的是c#6或更高版本,则可以使用以下字符串插值进行打印。

Console.WriteLine($"current value of face: {face_value}");
Console.WriteLine($"current number of faces: {die_faces}");

因此您的最终程序将如下所示,

namespace DieRoller
{
    public class Die
    {
        public int die_faces = 6;
        public int face_value = 1;

        public Die(int faces)
        {
            die_faces = faces;
            RollDie();
            GetFaceValue();
            GetNumFaces();
        }

        public void RollDie()
        {
            Random rand = new Random();
            face_value = rand.Next(die_faces);
        }

        public void GetFaceValue()
        {
            Console.WriteLine("current value of face: {0}", face_value);
        }

        public void GetNumFaces()
        {
            Console.WriteLine("current number of faces: {0}", die_faces);
        }
    }

    public class Program
    {
        public static void Main()
        {
             Die myDie = new Die(7);
             Console.WriteLine();
        }
    }
}

答案 1 :(得分:0)

尝试以这种方式重写GetFacesVaueGetNumFaces

public void GetFaceValue()
{
    Console.WriteLine("current value of face: {0}", face_value);
}

public void GetNumFaces()
{
    Console.WriteLine("current number of faces: {0}", die_faces);
}

答案 2 :(得分:0)

一些注意事项:

尽管可以在同一行中声明和分配,但必须先声明该变量,然后才能使用和分配它。

int number = 1;    // Creates a new int variable and assigns the value
number = 2;        // Reassigns number to now equal 2
anotherNumber = 3; // Doesn't work because we didn't declare the variable

如果我们有:

int field;

private void Foo()
{
    int field = 2;    // This creates a different field than the one at the top
}
private void Bar()
{
    field = 2;       // This works because it's the same field that's already been declared at the top
                     // It is NOT the same field that's used in Foo
}

因此,您应该更改die_facesface_value的分配,以免在前面使用int

Console.WriteLine的格式错误

仅因为它正在编译,并不意味着它是正确的。您应该将代码更改为:

public void GetFaceValue()
{
    Console.WriteLine($"current value of face: {face_value}");
}

注意,这也将起作用:

public void GetFaceValue()
{
    Console.WriteLine("current value of face: {0}", face_value);
}

但这确实令人困惑:

public void Foo()
{
    Console.WriteLine("Health: {0}, Strength: {1}, Armor: {2}, Potions: {3}, Weapons: {4}", health, strength, armor, potions, weapons);
    // vs
    Console.WriteLine($"Health: {health}, Strength: {strength}, Armor: {armor}, Potions: {potions}, Weapons: {weapons}");
}

您可以在任何地方使用它:

string result = $"Health: {health}, Strength: {strength}, Armor: {armor}, Potions: {potions}, Weapons: {weapons}";

应该起作用的代码:

public class Die
{
    public int die_faces = 6;
    public int face_value = 1;

    public Die(int faces)
    {
        die_faces = faces;

        RollDie();
        GetFaceValue();
        GetNumFaces();
    }
    public void RollDie()
    {
        var rand = new Random();
        face_value = rand.Next(die_faces);
    }
    public void GetFaceValue() => Console.WriteLine($"current value of face: ", face_value);
    public void GetNumFaces()  => Console.WriteLine($"current number of faces: ", die_faces);
}

public class Program
{
    public static void Main()
    {
        var myDie = new Die(7);
    }
}

请注意,var die = new Die()Die die = new Die();完全相同,只是冗余程度较低,通常可以节省时间和空间,var dict = new Dictionary<int, string>()Dictionary<int, string> dict = new Dictionary<int, string>()