Программа заканчивается после основного метода - PullRequest
0 голосов
/ 22 сентября 2018

Я новичок в c #, и я пытался сделать простую программу калькулятора.Однако моя программа заканчивается сразу после выполнения основного метода.Как заставить другие методы работать тоже?

public static void Main(string[]args)
{
    string firstNum; 
    string secondNum;
    string mathOporaters;
}



public static void NumGather(string firstNum, string secondNum, string mathOporaters)
{
    Console.WriteLine("Please enter the symbol of the operation you wish to use.");
    mathOporaters = Console.ReadLine();
    if (mathOporaters == "+")
    {
        Console.WriteLine("Now enter the fist number you wish to use that oporater on");
        firstNum = Console.ReadLine();
        Console.WriteLine("And now enter the second number");
        secondNum = Console.ReadLine();

        double num1 = Convert.ToDouble(firstNum);
        double num2 = Convert.ToDouble(secondNum);
        char oporater = Convert.ToChar(mathOporaters);
        double answer = (num1 + num2);
    }
    else if (mathOporaters == "*")
    {
        Console.WriteLine("Now enter the fist number you wish to use that oporater on");
        firstNum = Console.ReadLine();
        Console.WriteLine("And now enter the second number");
        secondNum = Console.ReadLine();

        double num1 = Convert.ToDouble(firstNum);
        double num2 = Convert.ToDouble(secondNum);
        char oporater = Convert.ToChar(mathOporaters);
        double answer = (num1 * num2);
    }
    else if (mathOporaters == "/")
    {
        Console.WriteLine("Now enter the fist number you wish to use that oporater on");
        firstNum = Console.ReadLine();
        Console.WriteLine("And now enter the second number");
        secondNum = Console.ReadLine();
        double num1 = Convert.ToDouble(firstNum);
        double num2 = Convert.ToDouble(secondNum);
        char oporater = Convert.ToChar(mathOporaters);
        double answer = (num1 / num2);
    }
    else if (mathOporaters == "-")
    {
        Console.WriteLine("Now enter the fist number you wish to use that oporater on");
        firstNum = Console.ReadLine();
        Console.WriteLine("And now enter the second number");
        secondNum = Console.ReadLine();

        double num1 = Convert.ToDouble(firstNum);
        double num2 = Convert.ToDouble(secondNum);
        char oporater = Convert.ToChar(mathOporaters);
        double answer = (num1 - num2);

        else{
            Console.WriteLine("Im sorry, I don't understand the symbol you entered. Please use one of these: +,  -,  *, /");
        }
    }
}

1 Ответ

0 голосов
/ 22 сентября 2018

Вы упускаете очень простой факт.Я рекомендую прочитать эту статью MSDN .

Там вы можете прочитать:

Основной метод - это точка входа в приложение C #.(Для библиотек и служб не требуется метод Main в качестве точки входа.) При запуске приложения метод Main является первым вызванным методом.

Когда программа запускается, метод помещается в стек .Когда стек пуст, приложение заканчивается .

Как уже упоминалось, Main() метод вызывается первым и находится в нижней части стека.Итак, когда он заканчивается, приложение заканчивается.

Подводя итог, это естественное поведение.Если вы хотите запускать определенные вами методы, вам нужно вызывать их (например, вы можете прочитать this ).

Попробуйте это:

public static void Main(string[]args)
{
    string firstNum; 
    string secondNum;
    string mathOporaters;
    // method call
    NumGather(firstNum, secondNum, mathOporaters);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...