猜数字:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int MAX_Number = 100;
int game();
int main()
{
int replay = 0;
do {
game();
printf("要继续吗?继续请按1并回车,按任意键并回车将退出\n");
scanf_s("%d", &replay);
} while (replay == 1);
return 0;
}
int game() {
srand(time(NULL));
int random_number = rand() % (101);
int max_times = 7;
printf("我现在生成了一个100以内的正整数,请你猜猜看?你有%d次机会\n", max_times);
int user_number;
while (max_times) {
scanf_s("%d", &user_number);
--max_times;
if (user_number < random_number) {
printf("小了,你还有%d次机会\n", max_times);
}
else if (user_number > random_number) {
printf("大了,你还有%d次机会\n", max_times);
}
else if (user_number == random_number) {
printf("太棒了,你猜中了!\n");
return 0;
}
}
printf("很遗憾,你没有猜中,正确的数字是%d\n", random_number);
return 0;
}
石头剪刀布:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char *JD = "剪刀";
const char *ST = "石头";
const char *BU = "布";
const char *LIB[] = {JD, ST, BU};
int main()
{
int replay = 0;
srand(time(NULL));
do {
int i = rand() % 3;
int j;
while (true) {
printf("请输入1至3之间任意一个数字,其中1代表剪刀,2代表石头,3代表布。\n");
scanf_s("%d", &j);
if (j >= 1 and j <= 3) {
break;
}
else {
printf("你的输入错误!");
continue;
}
}
switch (j - 1) {
case (0):
printf("你出的剪刀!");
break;
case (1):
printf("你出的石头!");
break;
case (2):
printf("你出的布!");
break;
}
switch (i) {
case (0):
printf("我出的剪刀!");
break;
case (1):
printf("我出的石头!");
break;
case (2):
printf("我出的布!");
break;
}
if (j - 1 == 0 and i == 0) printf("平局!");
else if (j - 1 == 0 and i == 1) printf("我赢了!");
else if (j - 1 == 0 and i == 2) printf("你赢了!");
else if (j - 1 == 1 and i == 0) printf("你赢了!");
else if (j - 1 == 1 and i == 1) printf("平局!");
else if (j - 1 == 1 and i == 2) printf("我赢了!");
else if (j - 1 == 2 and i == 0) printf("我赢了!");
else if (j - 1 == 2 and i == 1) printf("你赢了!");
else if (j - 1 == 2 and i == 2) printf("平局!");
printf("要继续吗?继续请按1并回车,否则请按任意键后回车。");
scanf_s("%d", &replay);
} while (replay == 1);
}