有一个c#程序关闭

时间:2014-02-15 02:36:36

标签: c#

在我的程序中,如果bool option = false,我需要关闭程序。 “while(option == false)”语句位于代码的底部。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculator
{
class Program
{
    static void Main(string[] args)
    {
        double numA = 0;
        double numB = 0;
        double answer = 0;
        string functionType;
        string aLine;
        string bLine;
        string next;
        bool option = true;

        while (option == true)
        {

            Console.WriteLine("Enter a function type: ");
            functionType = Console.ReadLine();

            switch (functionType)
            {

                case "V":
                    Console.WriteLine(" +   Addition" + '\n' + " -   Subtraction" + '\n'                + " *   Multiplication" + '\n' + " /   Division" + '\n' + " %   Percent" + '\n' + " \\   Square Root" + '\n' + " ^   Exponent");
                    Console.WriteLine("Enter a function type: ");
                    functionType = Console.ReadLine();
                    break;

                case "v":
                    Console.WriteLine(" +   Addition" + '\n' + " -   Subtraction" + '\n' + " *   Multiplication" + '\n' + " /   Division" + '\n' + " %   Percent" + '\n' + " \\   Square Root" + '\n' + " ^   Exponent");
                    Console.WriteLine("Enter a function type: ");
                    functionType = Console.ReadLine();
                    break;

            }

            Console.WriteLine("Enter the first number: ");
            aLine = Console.ReadLine();
            numA = Double.Parse(aLine); Console.WriteLine("Enter the second number: ");

            switch (functionType)
            {

                case "\\":
                    answer = Math.Sqrt(numA);
                    Console.WriteLine("The answer is: " + answer + '\n');

            Console.WriteLine("Would you like to do more calculations? (Y/N): ");
            next = Console.ReadLine();

            switch (next)
            {
                case "Y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "N":
                    option = false;
                    break;

                case "n":
                    option = false;
                    break;

            }
            continue;
                    break;

                case "%":
                    answer = numA * 100;
                    Console.WriteLine("The answer is: " + answer + '\n');

            Console.WriteLine("Would you like to do more calculations? (Y/N): ");
            next = Console.ReadLine();

            switch (next)
            {
                case "Y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "N":
                    option = false;
                    break;

                case "n":
                    option = false;
                    break;

            }
                    break;
                    continue;

            }

            Console.WriteLine("Enter the second number: ");
            bLine = Console.ReadLine();
            numB = Double.Parse(bLine);

            switch (functionType)
            {

                case "+":
                    answer = numA + numB;
                    break;

                case "-":
                    answer = numA - numB;
                    break;

                case "x":
                    answer = numA * numB;
                    break;


                case "*":
                    answer = numA * numB;
                    break;


                case "X":
                    answer = numA * numB;
                    break;

                case "/":
                    answer = numA / numB;
                    break;

                case "^":
                    answer = Math.Pow(numA, numB);
                    break;
            }

            Console.WriteLine("The answer is: " + answer + '\n');

            Console.WriteLine("Would you like to do more calculations? (Y/N): ");
            next = Console.ReadLine();

            switch (next)
            {
                case "Y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "y":
                    option = true;
                    Console.WriteLine('\n');
                    break;

                case "N":
                    option = false;
                    break;

                case "n":
                    option = false;
                    break;

            }


        }

        while (option == false)
        {

        }

    }
}
}

请原谅我,如果这是一个简单的答案,但我已经学习了c#约2小时。

1 个答案:

答案 0 :(得分:4)

最后你不需要while (option == false)循环。您需要做的是在每次用户输入后检查值,并且仅在不满足条件时才继续。即,在每个switch语句之后:

if (!option)
    return;

这将退出外部while循环,然后Main函数将退出,从而结束您的程序。