【问题标题】:Invalid expression of term else术语 else 的无效表达
【发布时间】:2014-02-03 19:43:51
【问题描述】:

我正在创建一个井字游戏,对于再次游戏部分,我得到了一个无效的 else 表达,有人可以帮忙吗?在我添加介绍程序之前它起作用了 提前致谢

static void playAgainMsg(String message)
    {
        Console.WriteLine(message + "Do you want to play again? Y/N"); // Displaying the resukt of the game and asking the user if they would like to play again and tells them to input Y or N
        if 
        {
            (Console.ReadLine().Equals("Y")) // This reads the line to check the user input
            strPlaysAgain.Equals("Y");
            introduction(); <---- worked before this was added...
        }   
        else
        {
            strPlaysAgain.Equals("N"); // This Changes the value from the default value of Y to N so that it exits the While loop for PlaysAgain.
            Console.Clear(); //Clears the console so that the game board is removed.
            MainMenu();  // returns the user back to the introduction -  edit to be main menu with an exit feature, highscores
        }
    }

【问题讨论】:

    标签: c# command-line console console-application


    【解决方案1】:

    你错了条件

            if (Console.ReadLine().Equals("Y")) //<------------
            {
                 // This reads the line to check the user input
                strPlaysAgain.Equals("Y");
                introduction();
            }   
            else
            {
                strPlaysAgain.Equals("N"); // This Changes the value from the default value of Y to N so that it exits the While loop for PlaysAgain.
                Console.Clear(); //Clears the console so that the game board is removed.
                MainMenu();  // returns the user back to the introduction -  edit to be main menu with an exit feature, highscores
            }
    

    【讨论】:

      【解决方案2】:

      您不会使用 if 测试任何内容。另外,str1.Equals(str2) 是一个测试。您必须使用= 来分配值。

      static void playAgainMsg(String message)
              {
                  Console.WriteLine(message + "Do you want to play again? Y/N"); 
                  if (Console.ReadLine().Equals("Y"))
                  {
                      strPlaysAgain = "Y";
                      introduction();
                  }   
                  else
                  {
                      strPlaysAgain = "N"; 
                      Console.Clear(); removed.
                      MainMenu();  
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 2011-06-22
        • 2020-09-23
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-10
        相关资源
        最近更新 更多