为什么我的totalTokens变量在循环结束时输出错误?

时间:2017-11-08 07:48:13

标签: c# function variables

为什么我的totalTokens变量在循环结束时得到错误的输出?如果我为每次猜测下注相同的金额,它会给我正确的输出,但如果用户在每次猜测时下注不同的金额,则会提供错误的输出。

int returnValue = random.Next(1, 100);
string input;
int guess = 0;
int count = 0;
int tokens = 1000;
int bet = 0;
int totalTokens;
int betResult = 0;
int countB = 0;
int victory = 50;           

Console.WriteLine("What is your bet?");
bet = Convert.ToInt32(Console.ReadLine());

if (guess >= 1 && guess <= 100)
{
    if (guess > returnValue)
    {
        Console.Beep(100,2000);
        Console.WriteLine("Guess Again! Your guess is too HIGH");
        betResult = - bet;
        countB += 1;
        count += 1;                 
    }
    if (guess < returnValue)
    {                        
        Console.Beep(300,2000);
        Console.WriteLine("Guess Again! Your guess is too LOW");
        betResult = -bet;
        countB += 1;
        count += 1;                     
    }
    if (guess == returnValue)
    {
        totalTokens = tokens + (betResult * countB) + victory;
        Console.Beep(50,2000);
        Console.Beep(60,2000);
        Console.Beep(70,2000);
        Console.WriteLine("You got it RIGHT!!!!!!");
        count += 1;
        Console.WriteLine("Your guess of " +returnValue+ " was right.");
        Console.WriteLine("took you " +count+ " guesses to win the game.");
        Console.WriteLine("You won {0} tokens",victory);
        Console.WriteLine("Your total tokens is {0}", totalTokens);
    }                   
}

while (guess != returnValue)
{
    Thread.Sleep(500000);
    Console.Clear();
}

1 个答案:

答案 0 :(得分:0)

此行实际上将betResult指定为最后一次下注:

betResult = -bet;

你应该写:

betResult -= bet;