简易控制台应用程序计算器

时间:2016-12-15 15:17:28

标签: c# string visual-studio while-loop

我已经测试了一个简单的计算器功能作为我的C#研究的一部分,我遇到了一个问题,即使我键入正确的选择,它也不会退出while循环。 这是代码:

static void Main(string[] args)
    {
        string calc, inputX, inputY, inputZ;
        double x, y, z;

        Console.Write("Welcome to the cool calculator. Please choose between sumCalc or multiCalc: ");
        calc = Console.ReadLine();

        while (calc != "sumCalc" || calc != "sumCalc")
        {
            Console.Write("Please type in the right calculator again: ");
            calc = Console.ReadLine();
        }

        if (calc == "sumCalc")
        {
            Console.WriteLine("You are now working with the sumCalc.");
            //Getting user input for the variable 'x'
            Console.WriteLine("Please enter a value for the first number:");
            inputX = Console.ReadLine();
            x = Convert.ToDouble(inputX);
            //Getting user input for the variable 'y'
            Console.WriteLine("Please enter a value for the second number:");
            inputY = Console.ReadLine();
            y = Convert.ToDouble(inputY);
            //Getting user input for the variable 'z'
            Console.WriteLine("Please enter a value for the third number:");
            inputZ = Console.ReadLine();
            z = Convert.ToDouble(inputZ);
            Console.WriteLine("The result is:" + sumCalc(x, y, z));
            Console.ReadKey();
        }
        else if (calc == "multiCalc")
        {
            Console.WriteLine("You are now working with the multiCalc.");
            //Getting user input for the variable 'x'
            Console.WriteLine("Please enter a value for the first number:");
            inputX = Console.ReadLine();
            x = Convert.ToDouble(inputX);
            //Getting user input for the variable 'y'
            Console.WriteLine("Please enter a value for the second number:");
            inputY = Console.ReadLine();
            y = Convert.ToDouble(inputY);
            //Getting user input for the variable 'z'
            Console.WriteLine("Please enter a value for the third number:");
            inputZ = Console.ReadLine();
            z = Convert.ToDouble(inputZ);
            Console.WriteLine("The result is:" + multiCalc(x, y, z));
            Console.ReadKey();
        }
    }

    //This is the multiply calculator function
    static double sumCalc(double x, double y, double z)
    {
        double res = x + y + z;
        return res;
    }

    //This is the multiply calculator function
    static double multiCalc(double x, double y, double z)
    {
        double res = x * y * z;
        return res;
    }

我不知道为什么会这样做,它应该可以正常工作。 请帮帮我,谢谢! :)

3 个答案:

答案 0 :(得分:2)

sumCalc条件下while两次。同时将||更改为&&,如果您使用OR作为条件,即使您输入sumCalcmultiCalc,循环也将为真,并继续询问你再次进入。

while (calc != "sumCalc" && calc != "multiCalc")

答案 1 :(得分:1)

尝试更改代码:

while (calc != "sumCalc" && calc != "multiCalc")

而不是:

while (calc != "sumCalc" || calc != "sumCalc")

答案 2 :(得分:-2)

您可以将循环条件更改为:

while (calc != "sumCalc" || calc != "multiCalc")