- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
绞刑吏有问题,请找出来??输入代码时的字母显示“未找到”,并且必须输入相同的字母两次才能接受它?猜出该字母的机会减少了如何解决?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define WORD_COUNT 3
#define MAX_LENGTH 10
typedef char string[MAX_LENGTH];
void main(void) {
string words[WORD_COUNT] = { "bird","fish","lion","ants","bear","deer","fowl" };
char answer[MAX_LENGTH];
char guess;
int count = -0, index, i, found, choice = -7;
char mysteryWord[MAX_LENGTH];
printf("Welcome to Hangman!\n");
printf("\n\nChoose an option\n"
"1) Easy\n"
"2) Moderate\n"
"3) Hard\n"
"Your choice: ");
scanf("%i", &choice); a biref menu case
switch (choice) {
case 1:
count = 5;
break;
case 2:
count = 2;
break;
case 3:
count = 1;
}
srand(time(NULL));
index = rand() % WORD_COUNT;
strcpy(mysteryWord, words[index]);/*actual comparing */
for (i = 0; i < strlen(mysteryWord); i = i + 1)
{
answer[i] = '-';
}
answer[i] = '\0';
printf("%s \n", answer);
while (1 > 0) {
printf("\n %i guess(es) left\n", count);
printf("Guess a letter:");
scanf("%c\n", &guess);
guess = tolower(guess);
found = 0;
for (i = 0; i < strlen(mysteryWord); i++)
{
if (mysteryWord[i] == guess) {
answer[i] = guess;
found = 1;
}
}
if (found == 0) {
printf("Not found!\n");
--count;
}
if (count == 0) {
printf("Game over\n");
printf("The answer is %s.", mysteryWord);
break;
}
else {
what should be here instead of if(answer==mysteryWord) ?
if (strcmp(answer, mysteryWord) == 0)
{
printf("Yes, it's a %s\n", answer);
break; /* or return */
} else
printf("%s", answer);
}
} end of while loop ?
} end of main ?
最佳答案
改变
scanf("%c\n", &guess);
至
scanf(" %c", &guess);
注意%c
之前的空格。空格会丢弃所有空格,例如换行符和空格,然后 %c
将扫描下一个非空格字符。
在您的情况下,当您为任何 scanf
输入数据时,您输入数据并按 Enter 键。scanf
读取输入的数据并留下
(换行符)。当您使用 stdin
中的\n%c
扫描字符时,scanf
会读取上一个 scanf
留下的 \n
,从而,不等待输入。
关于c - 最后如何完善C语言的hangman循环问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648031/
昨天发布于: MySQL: Finding most frequently occuring values in table 但想知道是否有办法改进答案,因为您不必在最后重复代码来获取MAX(COUN
在开发了一个展示基本智能的简单算法后,我渴望进行递归 self 改进,我遇到的问题是由于我对递归缺乏理解。 我明白,如果我有一些东西来评估我正在使用的算法的“适应性”以进行改进(我为算法提供其自身的二
我们使用二进制 (16) 字段来存储 IP 地址。我们这样做是因为它可以同时保存 IPv4 和 IPv6 地址,并且可以轻松地与 .Net IPAddress 类一起使用。 但是,我创建了以下 SQL
我在使用 wxPython 时遇到了一些纯粹的外观问题。例如,标签和它们所代表的控件之间的关系——一切看起来总是至少偏离目标 2-3 个像素,有时甚至更糟。在我当前正在创建的对话中,我已将文本控件的字
我正在开发一个具有 UICollectionView 的应用程序 - Collection View 的工作是显示来自网络服务的数据。 我正在尝试实现的应用程序的一个功能是使用户能够将此 UIColl
我(终于!)找到了一种在玻璃上呈现 Windows.Forms 控件的方法,它似乎没有任何重大缺点,也没有任何大的实现时间。它的灵感来自 this article来自 Coded,它基本上解释了如何
我是一名优秀的程序员,十分优秀!