无法使循环工作

时间:2017-06-04 20:10:57

标签: c#

如果用户键入"是"以外的任何内容,我正在使用while循环。或"否"它会告诉他们输入新内容,但每当他们输入其他内容时,只需要垃圾邮件"这不是一个选项"而不是从顶部开始。有人能解释一下为什么吗?提前谢谢。

              using System;

class CalculatorProgram
{
    //varibale for do-while loop
    private static string endAnswer;
    public static void Main() // <----- The Entry point
    {
     //Variables
     string Choice1;
    string mathChoice;
    decimal Num1;
    decimal Num2;
    decimal Answer;
    bool userWrong = true;

    Console.Write("Would you like to use Lane's Custom Calculator?(Yes/No): ");
    Choice1 = Console.ReadLine();

    while(userWrong)
    {
        if (Choice1 == "Yes")

        {
            do
            {
                Console.Write("Would you like to Add, Subtract, Multiply, or Divide? (Case Sensitive): ");
                mathChoice = Console.ReadLine();

                //User inputs the 2 numbers


                //Math Choices
                if (mathChoice == "Add")
                {
                    Console.WriteLine("What 2 numbers would you like to use?");
                    Console.Write("Number 1 is: ");
                    Num1 = decimal.Parse(Console.ReadLine());

                    Console.Write("Number 2 is: ");
                    Num2 = decimal.Parse(Console.ReadLine());

                    Answer = Num1 + Num2;
                    Console.WriteLine("Your expression is: " + Num1 + " + " + Num2 + " = " + Answer);
                }

                else if (mathChoice == "Subtract")
                {
                    Console.WriteLine("What 2 numbers would you like to use?");
                    Console.Write("Number 1 is: ");
                    Num1 = decimal.Parse(Console.ReadLine());

                    Console.Write("Number 2 is: ");
                    Num2 = decimal.Parse(Console.ReadLine());
                    Answer = Num1 - Num2;
                    Console.WriteLine("Your expression is: " + Num1 + " - " + Num2 + " = " + Answer);
                }

                else if (mathChoice == "Multiply")
                {
                    Console.WriteLine("What 2 numbers would you like to use?");
                    Console.Write("Number 1 is: ");
                    Num1 = decimal.Parse(Console.ReadLine());

                    Console.Write("Number 2 is: ");
                    Num2 = decimal.Parse(Console.ReadLine());
                    Answer = Num1 * Num2;
                    Console.WriteLine("Your expression is: " + Num1 + " X " + Num2 + " = " + Answer);
                }

                else if (mathChoice == "Divide")
                {
                    Console.WriteLine("What 2 numbers would you like to use?");
                    Console.Write("Number 1 is: ");
                    Num1 = decimal.Parse(Console.ReadLine());

                    Console.Write("Number 2 is: ");
                    Num2 = decimal.Parse(Console.ReadLine());
                    Answer = Num1 / Num2;
                    Console.WriteLine("Your expression is: " + Num1 + " / " + Num2 + " = " + Answer);
                }

                else
                {
                    Console.WriteLine("This is not an option! Shutting Down..");
                    Console.ReadKey();
                    Environment.Exit(0);
                }


                //varibale for while loop to continue if selected Yes.
                Console.Write("Another Equation?: ");
                endAnswer = Console.ReadLine();



            } while (endAnswer == "Yes");


            //Goodbye Message
            Console.WriteLine("Thank you for using my program, goodbye ");
            Console.ReadKey();
            Environment.Exit(0);
            userWrong = false;
        }
        //If someone selects no for wanting to use my program.
        else if (Choice1 == "No")
        {
            Console.WriteLine("Thank you for using my program, goodbye ");
            Console.ReadKey();
            Environment.Exit(0);
        }
        else
        {
            Console.WriteLine("That is not an option");
            Console.ReadLine();


        }

    } 





    }
}

2 个答案:

答案 0 :(得分:2)

程序正在等待用户在打印出“这不是一个选项”之后输入新的答案,你只是从不提示它。您也永远不会根据他们的新输入再次存储Choice1,因此它会始终检查您的do-while循环中的第一个if条件,以及它们最初输入的内容。

要解决此问题,请将else分支机构更改为此类内容。

//...
else
{
    Console.WriteLine("That is not an option");
    // reprompt the user so they know to type something in
    Console.Write("Would you like to use Lane's Custom Calculator?(Yes/No): ");
    // store the new choice to recheck next loop iteration
    Choice1 = Console.ReadLine();
}

答案 1 :(得分:-1)

可能你的问题是你没有正确验证输入。 现在,您从控制台读取输入并与静态字符串进行比较 “添加”与“添加”不同,与“添加(空格)”不同。 我建议你做一个更强大的输入验证:

Console.Write("Would you like to Add, Subtract, Multiply, or Divide? (Case Sensitive): ");
mathChoice = Console.ReadLine();
mathChoice = mathChoice.ToUpper().Trim();
//User inputs the 2 numbers

//Math Choices
if (mathChoice == "ADD")
{
     Console.WriteLine("What 2 numbers would you like to use?");
     ....
}