【问题标题】:Getting user input from a bool method and using it in a different bool method从 bool 方法获取用户输入并在不同的 bool 方法中使用它
【发布时间】:2021-03-10 19:12:12
【问题描述】:

所以我有一个 bool 方法来检查用户是否有俱乐部会员资格,然后我有另一个 bool 方法应该告诉如果用户确实有会员资格应该打印什么并玩小游戏来检查是否用户赢得奖品。问题是,当我运行程序时,它会打印出“欢迎来到 VIP,先生”。两次并跳过其余代码,所以我该怎么做才能解决这个问题。

static void Main(string[] args)
{
    LoyaltyDiscount();
    EnterContest(RandomClass);
}
    private static bool LoyaltyDiscount()
    {
        string input;
        Write("\nAre you a member of our Futility Club frequent shopper program? ('Y' if yes) ");
        input = ReadLine().ToUpper();
        if(input == "YES" || input == "Y")
        {
            //if user does have a club membership
            WriteLine("Welcome to the VIP only, sir.");
            return true;
        }
        else
        {
            //if user does not have a club membership
            WriteLine("Thats okay, we still appreciate your purchase.");
            return false;
        }
    }
    private static bool EnterContest(Random chanceGenerator)
    {
        string input = LoyaltyDiscount().ToString();
        int randomNumber;
        const int gameMin = 1;
        const int gameMax = 10;
        Random RandomClass = new Random();
        randomNumber = RandomClass.Next(gameMin, gameMax);

        if ((input == "YES") || (input == "Y"))
        {
            //prompt if user answers "yes" to if they a club membership and ask to play the game
            Write("Do you want to play a game and win a prize?");
            int.TryParse(ReadLine(), out int guess);
            if (guess < gameMin || guess > gameMax)
            {
                //if user guesses number out of boundary
                WriteLine("Guess out of boundary");
                return false;
            }
            if (guess == randomNumber)
            {
                //if user guesses correct number
                WriteLine("Correct guess.");
            }
            else
            {
                //if user guesses wrong number
                Write("Wrong guess.");
                return false;
            }
        }
        return true;
    }

目标是从 LoyaltyDiscount() 方法获取用户输入,并询问用户是否有会员资格,然后如果用户确实有会员资格,则仅当他们有会员资格时才在 EnterContest() 方法中使用它。

【问题讨论】:

    标签: c# methods boolean


    【解决方案1】:

    您在两个地方调用 LoyaltyDiscount() 方法,一次在 main 方法中,然后在 EnterContest() 方法中。这就是它打印两次的原因。

    【讨论】:

      【解决方案2】:

      1.您第一次在这里调用了“LoyaltyDiscount()”方法。

      static void Main(string[] args)
      {
          LoyaltyDiscount();
          EnterContest(RandomClass);
      }
      

      然后你又在这里调用它。这就是为什么它打印了两次。

      private static bool EnterContest(Random chanceGenerator)
      {
          string input = LoyaltyDiscount().ToString();
          .
          .
      }
      

      2。这个方法'LoyaltyDiscount()'的返回值为bool,它被转换为字符串类型为'True'或'False'。如果你打印 LoyaltyDiscount().Tostring() 的结果,你会看到发生了什么。

      string input = LoyaltyDiscount().ToString();
      ...Omit some codes...
      Console.WriteLine(input);//it will be either 'True' or 'False'
      //So here you could change the 'if' condition to be like this:
      //if (input=="True")
      
      if ((input == "YES") || (input == "Y"))
      {
       .......
      }
      

      【讨论】:

      • 我不认为这对代码有任何改变,我使用您推荐的解决方案运行它,它只是做了同样的事情并跳过了“if (input == "true")”下面的所有内容部分。
      猜你喜欢
      • 2019-07-08
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2011-10-14
      相关资源
      最近更新 更多