gpt4 book ai didi

c - "Guess my number"游戏中显示错误

转载 作者:行者123 更新时间:2023-11-30 16:11:36 24 4
gpt4 key购买 nike

我正在学习 C 编程,现在我正在尝试编写一个名为“猜我的数字”的程序,其中玩家 1 将在一个范围内选择一个数字,然后玩家 2 将猜测该数字。如果猜测次数超过 10 次,则玩家 1 获胜。然而,代码程序在 10 次尝试后中断,并且不显示“玩家 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");
}
}

最佳答案

您应该在检查 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;
}

关于c - "Guess my number"游戏中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com