c#为什么在我的代码中赢得不同类别的变量总和?

时间:2015-06-27 04:50:10

标签: c#

    public static void Main(string[] args)
    {
        string name = "asfd";
        int plhp = 100, plmp = 100, zenhp = 500;
        Random rdn = new Random();
        int atk = rdn.Next(10, 55);
        int zatk = rdn.Next(20, 35);

        while (plhp > 0 && zenhp > 0)
        {
            string action = Console.ReadLine();
            if (String.Equals(action, "attack", StringComparison.OrdinalIgnoreCase))
            {
                zenhp -= atk;
                Console.WriteLine("Zen has taken -" + atk.ToString() + " damage!");
                actionofzen(plhp, name, zenhp, zatk);
                Console.WriteLine("Your next move?");
            }
            else if (!(action == "attack" ))
            {
                Console.WriteLine("Invalid command.");
            }
        }
    }
    public static int actionofzen(int plhp, string name, int zenhp, int zatk)
    {
        Random rdn = new Random();
        string[] zenmoves = { "attack" };
        string zenaction = zenmoves[rdn.Next(zenmoves.Length)];
        if (zenaction == "attack")
        {
            plhp -= zatk;
            Console.WriteLine("Zen has countered, inflicting -" + zatk + " damage on " + name + ".");
        }
        return 0;
    }

问题在于,每当Zen做某事时,它都不会影响整体HP,就像plhp -= zatk;甚至不做任何事情一样,只有玩家可以影响Zen的健康状况。如何从不同的类中总结出这些变量?另外,如何在保持StringComparison.OrdinalIgnoreCase

的使用的同时简化这一过程
if (action.Equals("attack", StringComparison.OrdinalIgnoreCase) ||
    action.Equals("heal", StringComparison.OrdinalIgnoreCase))

2 个答案:

答案 0 :(得分:1)

你必须通过referance传递价值。所以它可以更新main方法中的更改。用ref关键字传递plhp这里是它的工作小提琴https://dotnetfiddle.net/amHbXF

public static void Main()
    {
        Console.WriteLine("Hello World");
        string name = "asfd";
        int plhp = 100, plmp = 100, zenhp = 500;
        Random rdn = new Random();
        int atk = rdn.Next(10, 55);
        int zatk = rdn.Next(20, 35);
        while (plhp > 0 && zenhp > 0)
        {
            string action = Console.ReadLine();
            if (String.Equals(action, "attack", StringComparison.OrdinalIgnoreCase))
            {
                zenhp -= atk;
                Console.WriteLine("Zen has taken -" + atk.ToString() + " damage!");
                actionofzen(ref plhp, name, zenhp, zatk);
                Console.WriteLine("Player HP: " +plhp);
                Console.WriteLine("Zen HP: " +zenhp);
                Console.WriteLine("Your next move?");

            }
            else if (!(action == "attack"))
            {
                Console.WriteLine("Invalid command.");
            }
        }
    }

    public static int actionofzen(ref int plhp, string name, int zenhp, int zatk)
    {
        Random rdn = new Random();
        string[] zenmoves =
        {
        "attack"
        }

        ;
        string zenaction = zenmoves[rdn.Next(zenmoves.Length)];
        if (zenaction == "attack")
        {
            plhp -= zatk;
            Console.WriteLine("Zen has countered, inflicting -" + zatk + " damage on " + name + ".");
        }

        return 0;
    }

答案 1 :(得分:0)

有关plhp的信息,请参阅https://msdn.microsoft.com/en-us/library/14akc2c7.aspx。为了修改plhp以传播回调用者,它必须作为ref类型传递。您的代码将其作为“副本”(值类型的默认值)传递,这意味着您可以在actionofzen中更改所有内容,并且调用者将永远不会看到更改。 plhp将具有与调用方法之前相同的值。

我不知道有什么方法可以简化字符串比较。你可以将两个字符串放下来,只使用==运算符,但我不确定这是否更简单。