猜数字游戏,如何使用 while 循环添加或显示剩余的猜测次数

Guess a number game , How to add or display number of guesses left using while loop

用户猜测 0-20 之间的数字的猜数游戏,我希望它显示用户还剩多少次尝试,例如最大尝试次数为 5 次,如果用户在第一次尝试时猜错了它会显示类似“ 剩余尝试次数:4”,我如何在我的代码中实现它?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    time_t t;
    srand((unsigned)time(&t));
    int randomNumber = rand() % 21;
    int num;
    int guess = 0;

    printf("\nThis is a guessing game.");
    printf("\nGuess 5 times only\n");

    printf("\nEnter your guess:");
    scanf("%d",&num);

    while (num != randomNumber && guess < 5){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5

        if(num > randomNumber){ // checks if num is greater than randomNumber
            printf("Too high! try again:");
            scanf("%d",&num);
            guess++;
        }

        if(num < randomNumber){ //checks if num is less than randomNumber
            printf("Too low! try again:");
            scanf("%d",&num);
            guess++;
        }

        if(num == randomNumber){ //checks if num is equal to randomNumber
            printf("You got it right!\n");
            return 0;
        }

        if(guess == 5) //checks if tries is 5 then exits program.
        {
            printf("your out guess of guesses!\n");
            return 0;
        }

    }

    return 0;
}


您可以将其添加到 while 循环中(最后)

// For total no of guesses = 5, if that is n then n - 1 - guess  
printf("tries left %d\n", 4 - guess);

如果您尝试在循环中打印一个值,printf(“You have %d guesses left\n”,5-guess); 可能会起作用

你已经有了答案,但我建议你在你的语句和你的程序中使用符号常量 #define GUESS_NUM 4 而不是实际数字,在这样的程序中它没有区别,但你可以得到在较大的项目中很容易混淆。

此外,您的循环应该在猜测达到 4 时结束,因为您在循环之前已经有一个猜测。你可以这样写:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define GUESS_NUM 4

int main()
{
    time_t t;
    srand((unsigned)time(&t));
    int randomNumber = rand() % 21;
    int num;
    int guess = 0;

    printf("\nThis is a guessing game.");
    printf("\nGuess 5 times only\n");

    printf("\nEnter your guess:");
    scanf("%d",&num);

    while (num != randomNumber && guess < GUESS_NUM){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5
        
        if(num > randomNumber){ // checks if num is greater than randomNumber
            printf("tries left : %d\n", GUESS_NUM-guess);
            printf("Too high! try again:");
            scanf("%d",&num);
            guess++;
        }

        if(num < randomNumber){ //checks if num is less than randomNumber
            printf("tries left : %d\n", GUESS_NUM-guess);
            printf("Too low! try again:");
            scanf("%d",&num);
            guess++;
        }

        if(num == randomNumber){ //checks if num is equal to randomNumber
            printf("You got it right!\n");
            return 0;
        }

        if(guess == GUESS_NUM) //checks if tries is 5 then exits program.
        {
            printf("your out guess of guesses!\n");
            return 0;
        } 
        
        
    }

    return 0;
}