尝试学习C#,我对这个VendingMachine问题感到沮丧。

这就是我到目前为止所得到的。

class VendingMachine
{
    public int Coke = 20;
    public int Beer = 20;
    public int LemonLime = 20;
    public int Grape = 20;
    public int CreamSoda = 20;

    public void VmContent()
    {
        Console.WriteLine("Choose a Drink.");
        Console.WriteLine("1. Coke - $1.00\n" +
                          "2. Beer - $1.00\n" +
                          "3. Lemon Lime - $1.00\n" +
                          "4. Grape - $1.50\n" +
                          "5. Cream Soda - $1.50\n");
    }

    public void Math()
    {
        var input = Console.ReadKey();

        switch (input.Key)
        {
            case ConsoleKey.D1:
                var i = Beer - 1;
                i = Beer;
                Console.WriteLine("You Got a Coke");
                Console.WriteLine($"Total Coke Left: {i}");
                break;

        }
    }

}


我只是想弄清楚如何从每个内容的总数中减去。
 而我在交换机中到达那里。
真的没有用。

最佳答案

您想从主变量中减去,像这样:

Coke--; //Or Coke = Coke - 1;
Console.WriteLine("You Got a Coke");
Console.WriteLine($"Total Coke Left: {Coke}");
break;

关于c# - 吸引公众参与,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51381472/

10-10 12:43