How to take variable declared in "if, switch, loops," etc

时间:2019-02-18 00:30:42

标签: c# .net

I made a program that will allow users to choose from 4 options of a shop.

  1. sword.
  2. shield
  3. potions
  4. Leave/Checkout is supposed to re-read their input and gather the total "gold".

But it won't let me grab the variables I made out of the other options(the quantities).

I tried if statements and I'm currently working on using a switch statement instead.

switch (input)
{
    case (1):
        Console.WriteLine("How many swords would you like to buy?");
        quantity1 = Convert.ToInt32(Console.ReadLine());
        swgold = 5 * quantity1;
        break;

    case (2):
        Console.WriteLine("How many shields would you like to buy?");
        quantity2 = Convert.ToInt32(Console.ReadLine());
        shgold = 8 * quantity2;
        break;

    case (3):
        Console.WriteLine("How many potions would you like to buy?");
        quantity3 = Convert.ToDouble(Console.ReadLine());
        pgold = 3 * quantity3;
        break;

    case (4):
        Console.WriteLine($"{quantity1} Swords {swgold} gold");
        Console.WriteLine($"{quantity2} shields {shgold} gold");
        Console.WriteLine($"{quantity3} potions {pgold} gold");
        Console.ReadLine();
        Leave = true;
        break;
}

I need the Console.WriteLine($"{quantity1} to let me actually use the quantity variables from the other cases.

1 个答案:

答案 0 :(得分:2)

It looks like you use method local varaible and you can't save data to use ,so you can try to use Property.

ex:

class User
{
    public int MyProperty { get; set; }
    public int quantity1 { get; set; }
    public int swgold { get; set; }
    public int quantity2 { get; set; }
    public int shgold { get; set; }
    public double quantity3 { get; set; }
    public double pgold { get; set; }
    public bool Leave { get; set; }
    public void Buy(int input)
    {

        var Leave = false;

        switch (input)
        {
            case (1):
                Console.WriteLine("How many swords would you like to buy?");
                quantity1 = Convert.ToInt32(Console.ReadLine());
                swgold = 5 * quantity1;
                break;


            case (2):

                Console.WriteLine("How many shields would you like to buy?");
                quantity2 = Convert.ToInt32(Console.ReadLine());
                shgold = 8 * quantity2;
                break;


            case (3):

                Console.WriteLine("How many potions would you like to buy?");
                quantity3 = Convert.ToDouble(Console.ReadLine());
                pgold = 3 * quantity3;
                break;

            case (4):

                Console.WriteLine($"{quantity1} Swords {swgold} gold");
                Console.WriteLine($"{quantity2} shields {shgold} gold");
                Console.WriteLine($"{quantity3} potions {pgold} gold");
                Console.ReadLine();
                Leave = true;
                break;
        }
    }
}

Call & Result:

void Main()
{
    var user = new User();
    user.Buy(1);
    user.Buy(2);
    user.Buy(3);
    user.Buy(4);
}

enter image description here