我正在学习C语言编程,现在我想对这个名为“猜我的号码”的程序进行编码,由此玩家1将选择一个范围内的数字,然后玩家2将猜测该数字。如果猜测次数超过10次尝试,则玩家1将获胜。但是,该代码程序在尝试10次后中断,并且不显示“ Player 1获胜”。有人可以帮我吗?谢谢。
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
return 0;
if (count == maxguesses) {
printf("Player 1 wins");
}
}
答案 0 :(得分:0)
在检查count == maxguesses之后,应放置return 0语句。
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
if (count == maxguesses) {
printf("Player 1 wins");
}
return 0;
}