【发布时间】: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() 方法中使用它。
【问题讨论】: