【发布时间】:2019-12-19 22:45:56
【问题描述】:
嗨,我是 C 新手,我正在编写一个基本的刽子手游戏,当我运行代码时,即使之前的 if 语句触发了 else 语句,为什么会发生这种情况
代码如下:
#include <stdio.h>
int main(int argc, char const *argv[]) {
int Guesses = 0;
int Completion = 0;
char UserGuess;
char Word[] = "fire";
while (Guesses < 6) {
if (Completion == 4) {
printf("You Won\n");
break;
}
printf("Please Enter Your Guess It Can Only Be A Letter Not A Word\n");
printf("The Word Is 4 Characters Long\n");
scanf("%c", &UserGuess);
if (UserGuess == 'f') {
printf("You Guessed One Of The Letters\n");
printf("f_ _ _\n");
Completion++;
}
if (UserGuess == 'i') {
printf("You Guessed One Of The Letters\n");
printf("_i_ _\n");
Completion++;
}
if (UserGuess == 'r') {
printf("You Guessed One Of The Letters\n");
printf("_ _r_ \n");
Completion++;
}
if (UserGuess == 'e') {
printf("You Guessed One Of The Letters\n");
printf("_ _ _e\n");
Completion++;
}
else {
printf("You Guessed The Letter Wrong try again\n");
Guesses++;
}
}
}
【问题讨论】:
-
你的意思可能是一串 if/else 语句,而不是一串 ifs
-
这和这里的问题一样:if-else statement
-
以及逻辑问题(假设有人猜测
f和i;你不会同时显示f和i- 目前,看起来像你' d 只显示最新猜测的字母),您可能应该使用" %c"输入UserGuess。这将跳过空格,例如换行符(以及空格和制表符)..
标签: c