在c#中获得无限循环-计算器应用程序初学者

在c#中获得无限循环-计算器应用程序初学者,c#,console,calculator,C#,Console,Calculator,我在上一个while循环中得到了无限循环,它的状态是:while(Valid3==false)。。。 我希望收到console.writeline(“无效输入,请重试”),直到用户满足参数,但当我输入无效输入时,我收到无限循环。虽然我觉得我在这里使用的逻辑与之前使用的循环中的逻辑相同。下面是我所有的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste

我在上一个while循环中得到了无限循环,它的状态是:while(Valid3==false)。。。 我希望收到console.writeline(“无效输入,请重试”),直到用户满足参数,但当我输入无效输入时,我收到无限循环。虽然我觉得我在这里使用的逻辑与之前使用的循环中的逻辑相同。下面是我所有的代码

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

namespace CalculatorApp
{
class Program
{
    static void Main(string[] args)
    {
        // Declare my and then initialize to zero.
        float num1 = 0; float num2 = 0;

        // Display title as the C# console calculator app.

        Console.WriteLine("Console Calculator in C#\r");
        Console.WriteLine("------------------------\n");

        // Ask the user to type the first number.
        //Console.WriteLine("Type a number, and then press Enter");


        Console.WriteLine("Type a number, and then press Enter");

        List<string> validChars = new List<string>() { "a", "s", "d", "m"};
        double negSqrt = Math.Sqrt(-1);
        bool Valid = false;
        bool Valid2 = false;
        bool Valid3 = false;
        float Number;
        while (Valid == false)
        {
            string Input = Console.ReadLine();
            if (!float.TryParse(Input, out Number))
            {
                Console.WriteLine("Not an integer, please try again.");
            }
            else
            {
                Valid = true;
                num1 = (float)Convert.ToDecimal(Input);
            }
        }

        // Ask the user to type the second number.
        Console.WriteLine("Type another number, and then press Enter");
        while (Valid2 == false)
        {
            string Input2 = Console.ReadLine();
            if (!float.TryParse(Input2, out Number))
            {
                Console.WriteLine("Not an integer, please try again.");

            }
            else
            {
                Valid2 = true;
                num2 = (float)Convert.ToDecimal(Input2);
            }
        }

        // Ask the user to choose an option.
        Console.WriteLine("Choose an option from the following list:");
        Console.WriteLine("\ta - Add");
        Console.WriteLine("\ts - Subtract");
        Console.WriteLine("\tm - Multiply");
        Console.WriteLine("\td - Divide");
        Console.Write("Your option? ");
        string o = Console.ReadLine();
        while (Valid3 == false)
        {
            foreach (string s in validChars)
            {
                if (!o.Contains(s))
                {
                    Console.WriteLine("Invalid input please try again");
                }
                else
                {
                    Valid3 = true;
                    switch (o)
                    {
                        case "a":
                            Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
                            break;
                        case "s":
                            Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
                            break;
                        case "m":
                            Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
                            break;
                        case "d":
                            Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
                            break;

                    }
                }
            }
        }
            // Use a switch statement to do the math.

            // Wait for the user to respond before closing.
            Console.Write("Press any key to close the Calculator console app...");
            Console.ReadKey();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间计算器
{
班级计划
{
静态void Main(字符串[]参数)
{
//声明my,然后初始化为零。
浮点num1=0;浮点num2=0;
//将标题显示为C#控制台计算器应用程序。
WriteLine(“C#\r中的控制台计算器”);
Console.WriteLine(“---------------------------\n”);
//请用户键入第一个数字。
//Console.WriteLine(“键入一个数字,然后按Enter”);
Console.WriteLine(“键入一个数字,然后按Enter”);
List validChars=新列表(){“a”、“s”、“d”、“m”};
双负Sqrt=Math.Sqrt(-1);
bool Valid=false;
bool-Valid2=假;
bool-Valid3=假;
浮点数;
while(Valid==false)
{
字符串输入=Console.ReadLine();
如果(!float.TryParse(输入,输出编号))
{
WriteLine(“不是整数,请重试。”);
}
其他的
{
有效=真;
num1=(浮点)转换为十进制(输入);
}
}
//请用户键入第二个数字。
Console.WriteLine(“键入另一个号码,然后按Enter”);
while(Valid2==false)
{
字符串Input2=Console.ReadLine();
如果(!float.TryParse(输入2,输出编号))
{
WriteLine(“不是整数,请重试。”);
}
其他的
{
Valid2=真;
num2=(浮点)将.ToDecimal转换为(Input2);
}
}
//请用户选择一个选项。
WriteLine(“从下面的列表中选择一个选项:”);
Console.WriteLine(“\ta-Add”);
Console.WriteLine(“\ts-Subtract”);
Console.WriteLine(“\tm-Multiply”);
Console.WriteLine(“\td-Divide”);
控制台。写(“你的选择?”);
字符串o=Console.ReadLine();
while(Valid3==false)
{
foreach(有效字符中的字符串s)
{
如果(!o.包含)
{
Console.WriteLine(“无效输入,请重试”);
}
其他的
{
Valid3=真;
开关(o)
{
案例“a”:
WriteLine($”您的结果:{num1}+{num2}=“+(num1+num2));
打破
案例“s”:
WriteLine($”您的结果:{num1}-{num2}=“+(num1-num2));
打破
案例“m”:
WriteLine($”您的结果:{num1}*{num2}=“+(num1*num2));
打破
案例“d”:
WriteLine($”您的结果:{num1}/{num2}=“+(num1/num2));
打破
}
}
}
}
//使用switch语句进行计算。
//在关闭前等待用户响应。
控制台。写入(“按任意键关闭计算器控制台应用程序…”);
Console.ReadKey();
}
}
}

在while循环中移动
控制台。ReadLine()

    while (Valid3) {
        string o = Console.ReadLine();
...
它为什么做无限循环


您正在请求一个输入,在接收该输入后,您进入while循环。如果输入无效,您将不再要求另一个输入,而是一次又一次地重复相同的输入。

您必须读取循环中的运算符,否则您将始终反复比较相同的无效输入。此外,您还可以简化测试。与根据有效输入列表进行测试不同,只需在switch语句中添加默认情况。如果其他情况均不匹配,则将执行该命令

bool isOperatorValid;
do {
    isOperatorValid = true;
    switch (Console.ReadLine()) {
        case "a":
            Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
            break;
        case "s":
            Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
            break;
        case "m":
            Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
            break;
        case "d":
            Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
            break;
        default:
            Console.WriteLine("Invalid input please try again");
            isOperatorValid = false;
            break;
    }
} while (!isOperatorValid);

isOperatorValid
Valid3
更清晰。我还使用了do while语句来测试循环结束时的条件,因为我们在开始循环时不知道其结果。

您正在使用字符串o=Console.ReadLine()读取输入;圈外!,把它放在后面,现在是熟悉调试器的最佳时机。此外,当您刚刚完成
TryParse
时,您不需要执行这种奇怪的转换/强制转换操作。你在做额外的工作。你的代码非常复杂。删除
validChars
变量和最后一条if-else语句,改为在switch语句中添加
default:
case。看:。谢谢大家哈哈!我和他们一样是新手。我应该先调试一下自己,我用过这个!我必须在每个有效案例之后添加isOperatorValid=true,以使其正常工作,因为如果我输入了无效响应,isOperatorValid的值将全局更改为false,这将打乱重试时的do while循环。