Visual C# - 当前上下文中不存在变量

时间:2017-08-06 05:13:37

标签: c#

对于编程c#,我是初学者,虽然我在main函数中声明了它们,但是公式中有两个变量没有解析为整数。我有多个方法实现小任务,它似乎没有将主变量链接到方法一,其余的代码没有任何错误,所以我不知道为什么它没有做公式。 (我必须发布所有代码才能显示你)

 public static void Main()
    {
        int income = 0;
        int children = 0;
        int tax = 0;
        AskForIncome(income);
        NoChild(children);
        CalculateTax(tax);
        ExitProgram();
    }

    public static void AskForIncome(int income)
    {
        Console.WriteLine("What is your total income: ");
        string testNum = Console.ReadLine();
        bool dummyBool = int.TryParse(testNum, out income);
        if (dummyBool)
        {
            Console.WriteLine();
        }
        else if(income < 0)
        {
            Console.WriteLine("Your income cannot be negative."); 
        }
        else
        {
            Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
        }
    }
    public static void NoChild(int children)
    {

        Console.WriteLine("How many children do you have: ");
        string testChild = Console.ReadLine();
        bool dummyBool2 = int.TryParse(testChild, out children);
        if (dummyBool2)
        {
            Console.WriteLine();
        }
        else if(children < 0)
        {
            Console.WriteLine("You must enter a positive number."); 
        }
        else
        {
            Console.WriteLine("You must enter a valid number.");
        }

    }
    public static void CalculateTax(int tax)
    {
        tax = income - 10000 - (children * 2000);
        if (tax < 0)
        {
            Console.WriteLine("You owe no tax.");
        }
        else
        {
            Console.WriteLine("You owe a total of $" + tax + " tax.");
        }
    }

1 个答案:

答案 0 :(得分:0)

c#它的oop语言,在你的例子中看起来像是用c开发的, 首先,你需要做一个类,它将包含你需要的所有变量和函数\方法:

public class Employ
{
  public int income {get; set;}
  public int children {get; set}
  public int tax {get; set};

          public static void AskForIncome()
        {
            Console.WriteLine("What is your total income: ");
            string testNum = Console.ReadLine();
            bool dummyBool = int.TryParse(testNum, out income);
            if (dummyBool)
            {
                Console.WriteLine();
            }
            else if (income < 0)
            {
                Console.WriteLine("Your income cannot be negative.");
            }
            else
            {
                Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
            }
        }
        public static void NoChild()
        {

            Console.WriteLine("How many children do you have: ");
            string testChild = Console.ReadLine();
            bool dummyBool2 = int.TryParse(testChild, out children);
            if (dummyBool2)
            {
                Console.WriteLine();
            }
            else if (children < 0)
            {
                Console.WriteLine("You must enter a positive number.");
            }
            else
            {
                Console.WriteLine("You must enter a valid number.");
            }

        }
        public static void CalculateTax()
        {
            tax = income - 10000 - (children * 2000);
            if (tax < 0)
            {
                Console.WriteLine("You owe no tax.");
            }
            else
            {
                Console.WriteLine("You owe a total of $" + tax + " tax.");
            }
        }
}

从主要创建雇用对象并做你需要的所有事情,但要了解oop编程它必须。