【问题标题】:Simple console application calculator functions简单的控制台应用程序计算器功能
【发布时间】:2017-05-01 06:10:05
【问题描述】:

作为我的 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;
    }

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

【问题讨论】:

  • 下次此类问题将被关闭。 Questions seeking debugging help ("why isn't this code working?") must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers
  • 知道了,下次不会了:)

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


【解决方案1】:

您在while 条件下有两次sumCalc。还要把||改成&&,如果你用OR作为条件,即使你输入sumCalc或multiCalc,循环也会为真,一直要求你再次输入。

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

【讨论】:

  • 是的,很抱歉,我输入的是它而不是 multiCalc :P
  • @Reniuz 如果你使用 OR,它将是无穷无尽的,因为循环总是正确的,我在发布答案之前确实测试了这个。
  • @Reniuz 想想OR的逻辑,0 || 1 = 1 但 AND 为 0 && 1 = 0。老实说,我不得不修改我的记忆。
  • 啊抱歉,你是对的。我的翻译很糟糕:D
【解决方案2】:

尝试更改您的代码:

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

而不是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    相关资源
    最近更新 更多