C语言实现猜数字小项目
项目介绍
在本项目中,我们将使用C语言编写一个简单的猜数字游戏。玩家需要在规定时间内猜出一个随机数字,每猜一次,游戏将会告知玩家数字大小是否正确。
实现步骤
步骤1:生成随机数字
我们首先需要生成一个随机数字,使用C语言标准库中的rand()函数可以生成介于0和RAND_MAX之间的随机数,通过取模运算将其转换为我们需要的数字范围。
int generateRandomNumber(int lowerLimit, int upperLimit){
return (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;
}
步骤2:处理用户输入
接下来,我们需要处理用户输入的数字。可以使用scanf()函数从控制台获取用户输入,并将其保存到一个变量中。
int userInput;
scanf("%d", &userInput);
步骤3:比较数字大小并提供反馈
我们需要将用户输入与随机数字进行比较,并告知用户猜测是否正确,或是数字偏大或偏小。
if(userInput == randomNumber){
printf("Congratulations! You guessed the number correctly!\n");
return 1;
}
else if(userInput < randomNumber){
printf("The number you guessed is too small. Please try again.\n");
}
else{
printf("The number you guessed is too large. Please try again.\n");
}
步骤4:完整代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int generateRandomNumber(int lowerLimit, int upperLimit){
return (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;
}
int main(){
// 设置随机种子
srand(time(NULL));
// 生成随机数字
int randomNumber = generateRandomNumber(1, 100);
// 游戏规则与提示
printf("Welcome to the Number Guessing Game!\n");
printf("The game generates a random number between 1 and 100. You have to guess this number correctly.\n");
printf("You will be given 10 attempts to guess the number.\n");
// 开始游戏
int numberOfAttempts = 0;
while(numberOfAttempts < 10){
printf("\nAttempt %d - Enter your guess: ", numberOfAttempts + 1);
// 处理用户输入
int userInput;
scanf("%d", &userInput);
if(userInput == randomNumber){
printf("Congratulations! You guessed the number correctly!\n");
return 0;
}
else if(userInput < randomNumber){
printf("The number you guessed is too small. Please try again.\n");
}
else{
printf("The number you guessed is too large. Please try again.\n");
}
numberOfAttempts++;
}
printf("Sorry! You have used up all your attempts! The correct number was %d.\n", randomNumber);
return 0;
}
示例说明
示例1:用户猜小了
Welcome to the Number Guessing Game!
The game generates a random number between 1 and 100. You have to guess this number correctly.
You will be given 10 attempts to guess the number.
Attempt 1 - Enter your guess: 50
The number you guessed is too small. Please try again.
Attempt 2 - Enter your guess: 75
The number you guessed is too small. Please try again.
Attempt 3 - Enter your guess: 85
The number you guessed is too small. Please try again.
Attempt 4 - Enter your guess: 95
The number you guessed is too small. Please try again.
Attempt 5 - Enter your guess: 99
The number you guessed is too small. Please try again.
Attempt 6 - Enter your guess: 100
Congratulations! You guessed the number correctly!
示例2:用户猜大了
Welcome to the Number Guessing Game!
The game generates a random number between 1 and 100. You have to guess this number correctly.
You will be given 10 attempts to guess the number.
Attempt 1 - Enter your guess: 50
The number you guessed is too large. Please try again.
Attempt 2 - Enter your guess: 25
The number you guessed is too large. Please try again.
Attempt 3 - Enter your guess: 13
The number you guessed is too large. Please try again.
Attempt 4 - Enter your guess: 7
The number you guessed is too large. Please try again.
Attempt 5 - Enter your guess: 3
The number you guessed is too large. Please try again.
Attempt 6 - Enter your guess: 1
Congratulations! You guessed the number correctly!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现猜数字小项目 - Python技术站