【问题标题】:Creating option trees in console applications在控制台应用程序中创建选项树
【发布时间】:2016-10-07 05:39:01
【问题描述】:

应该很清楚我要完成的工作,根据之前的选择制作不同的选项树。出于某种原因,它在我离开 cmets 的两个地方都抛出了预期的错误“}。我做错了什么吗?这甚至会像我想要的那样工作吗?

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            double TotalAmount;
            TotalAmount = 300.7 + 75.60;
            Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
            Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
            string UserInput2 = Console.ReadLine();
            if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?");
            {
                string UserInput = Console.ReadLine();
                double UserInput3 = double.Parse(UserInput);
                TotalAmount = TotalAmount - UserInput3;
                Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
                Console.WriteLine("Anything else?");
                string UserInput4 = Console.ReadLine();

                if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?");
                Console.WriteLine("1). Withdraw");
                Console.WriteLine("2). Deposit");
                Console.WriteLine("Use number key for selection");
                string UserInput5 = Console.ReadLine();
                if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?");
                else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?");
                string UserInput6 = Console.ReadLine(); //FIRST ERROR

            else if (UserInput4.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                {
                    System.Environment.Exit(1);
                }
            } //SECOND ERROR
            else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
            {
                System.Environment.Exit(1);
            }
        }
    }
}

【问题讨论】:

    标签: c# if-statement tree options


    【解决方案1】:

    您的问题是您的代码结构非常难以阅读,因此您在推理时遇到了问题。基本上,您没有正确关闭大括号。看看这个:

    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                double TotalAmount;
                TotalAmount = 300.7 + 75.60;
                Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
                Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
                string UserInput2 = Console.ReadLine();
                string UserInput4;
                if (UserInput2.ToLower() == "yes")
                {
                    Console.WriteLine("How much would you like to withdraw?");
                    string UserInput = Console.ReadLine();
                    double UserInput3 = double.Parse(UserInput);
                    TotalAmount = TotalAmount - UserInput3;
                    Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
                    Console.WriteLine("Anything else?");
                    UserInput4 = Console.ReadLine();
                    if (UserInput4.ToLower() == "yes")
                    {
                        Console.WriteLine("What do you want to do?");
                    }
                    Console.WriteLine("1). Withdraw");
                    Console.WriteLine("2). Deposit");
                    Console.WriteLine("Use number key for selection");
                    string UserInput5 = Console.ReadLine();
                    if (UserInput5.ToLower() == "1")
                    {
                        Console.WriteLine("How much would you like to withdraw?");
                    }
                    else if (UserInput5.ToLower() == "2")
                    {
                        Console.WriteLine("How much would you like to deposit?");
                    }
                    string UserInput6 = Console.ReadLine();
                }
                else if (UserInput2.ToLower() == "no")
                {
                    Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                    System.Environment.Exit(1);
                }
                else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                {
                    System.Environment.Exit(1);
                }
            }
        }
    }
    

    我建议,每当您有“if”或“else if”语句时,始终将其后面的代码块放在大括号中——即使它是单行的。它将为您节省很多痛苦。 另外,很抱歉,如果我在回答中弄乱了您的应用程序逻辑。但是您将变量命名为 UserInput1、UserInput2 等。如果您提供一些更有意义的名称,例如“wouldLikeToWithdrawAnswer”、“howMuchAnswer”等,会容易得多。我只是对所有这些变量有点迷失了 :)

    【讨论】:

    • 啊,有道理,抱歉,这一切还是很新的。复制/粘贴您的代码,它没有给出任何错误,但我尝试自己复制它并全部输入,但它没有用。如果这让我听起来像个白痴,我很抱歉,但是缩进的括号和行有多么重要?
    • dotnetfiddle.net/uKyiO7 这是我现在的样子,但仍然有大括号错误
    • 啊,没关系,我现在明白了。我还有“;”在几行的末尾。感谢您的帮助,非常感谢:)
    【解决方案2】:

    您编写代码的方式很难被别人理解。 你需要改进很多。这是工作的Example on .NET Fiddle

    public static void Main()
        {
                double TotalAmount;
                TotalAmount = 300.7 + 75.60;
                Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
                Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
                string UserInput2 = Console.ReadLine();
                if (UserInput2.ToLower() == "yes") 
                {
                    Console.WriteLine("How much would you like to withdraw?");
                    string UserInput = Console.ReadLine();
                    double UserInput3 = double.Parse(UserInput);
                    TotalAmount = TotalAmount - UserInput3;
                    Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
                    Console.WriteLine("Anything else?");
                    string UserInput4 = Console.ReadLine();
    
                    if (UserInput4.ToLower() == "yes")
                    {
                        Console.WriteLine("What do you want to do?");
                        Console.WriteLine("1). Withdraw");
                        Console.WriteLine("2). Deposit");
                        Console.WriteLine("Use number key for selection");
                        string UserInput5 = Console.ReadLine();
                        if (UserInput5.ToLower() == "1")
                        {
                            Console.WriteLine("How much would you like to withdraw?");
                        }
                        else if (UserInput5.ToLower() == "2")
                        {
                            Console.WriteLine("How much would you like to deposit?");
                            string UserInput6 = Console.ReadLine(); //FIRST ERROR
                        }
                    }
                else if (UserInput4.ToLower() == "no") 
                    {
                        Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                        System.Environment.Exit(1);
                    }
                } //SECOND ERROR
                else if(UserInput2.ToLower() == "no") 
                {
                    Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                    System.Environment.Exit(1);
                }
            }
    

    【讨论】:

      【解决方案3】:

      问题是你的if 块没有去你认为的地方:

      if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?");
      // And that's the if statement done.
              {
                  // This is not part of any if block
                  string UserInput = Console.ReadLine();
                  double UserInput3 = double.Parse(UserInput);
                  TotalAmount = TotalAmount - UserInput3;
                  Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
                  Console.WriteLine("Anything else?");
                  string UserInput4 = Console.ReadLine();
      
                  if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?");
                  Console.WriteLine("1). Withdraw");
                  Console.WriteLine("2). Deposit");
                  Console.WriteLine("Use number key for selection");
                  string UserInput5 = Console.ReadLine();
                  if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?");
                  else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?");
                  string UserInput6 = Console.ReadLine(); //FIRST ERROR
      
              else //Compiler says "else?  There is no if to match this with!"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多