所以一些信息,我正在做一个绞刑游戏,从一个数组中得到一个随机单词,然后这个随机词被蒙住,打印成'_‘,向用户显示这个随机单词有多少字母。
一个典型的刽子手是你有很多尝试,一旦你达到一定的门槛,你就输了。这是在代码中显示的打印出来的绞刑架,随着计数器的每次增加,慢慢地建立。
现在的问题在于我不知道如何将这些概念联系起来。扫描代码将如何与随机字和计数器交互。特别是由于蒙面词在int中,我尝试使用char,for循环;
int counter = 0; answer != m; counter++但是什么都没有发生,相反,它只是输出了“你赢了!”尽管只写了一封信。
我甚至试过;
int counter = 0; answer != 'q'; counter++因此,我可以看到,如果我输入一个字母,而不是Q,如果它将开始建设挂人,但即使这样,它仍然打印出“你赢了!”
也曾尝试过;
for (int counter = 0; counter >= 7; counter++)因此,它必须继续计数,直到大于或等于7,但仍然什么也没有。
有小费吗?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <string>
#define ARRAY_SIZE 10
int main()
{
//randomwordgenerator
char word[ARRAY_SIZE][200] = {
"tiger", "lion", "elephant", "zebra", "horse",
"camel", "deer", "crocodile", "rabbit", "cat"
};
int x = 0;
srand(time(0));
x = rand() % ARRAY_SIZE;
system("pause"); //will pause the rand function
// masking and unmasking word
char m = strlen(word[x]); //will count the number of letters of the random word
// int mask[200]{};
int mask[200]{};
for (int i = 0; i < m; ++i) { //loop until all leters are masked
mask[i] = '_';
printf("%c ", mask[i]);
}
//Introduction
printf("\nHey! Can you please save me? \n");
printf(" O\n/|\\ \n/ \\ \n");
//Ask for answer
printf(
"\n"
"Type a letter to guess the word and save me. Here are some tips!\n"
"1) The '_' above are how many letters make up the word, isn't that neat!\n"
"2) The letters are case sensitive so please pick lower case or I might die\n"
"3) Have fun! \nNow with that out of the way please type in your guess; "
);
char answer;
scanf_s("%c", &answer);
//loop w counter
for (int counter = 0; answer != 'q'; counter++) {
if (counter == 1) {
printf("\n=========");
}
else if (counter == 2) {
printf("\n+\n|\n|\n|\n|\n|\n=========");
}
else if (counter == 3) {
printf("\n+---+\n| |\n|\n|\n|\n|\n=========");
}
else if (counter == 4) {
printf("\n+---+\n| |\n| O\n|\n|\n|\n=========");
}
else if (counter == 5) {
printf("\n+---+\n| |\n| O\n| |\n|\n|\n=========");
}
else if (counter == 6) {
printf("\n+---+\n| |\n| O\n| |\n| / \\ \n|\n=========");
}
else if (counter == 7) {
printf("\n+---+\n| |\n| O\n| /| \n| / \\ \n|\n=========");
}
else if (counter == 8) {
printf("\n+---+\n| |\n| O\n| /|\\ \n| / \\ \n|\n=========");
}
else if (counter == 9) {
printf("\nReally left me hanging there buddy");
return 0;
}
else {
printf("\nThanks for saving me!");
}
return 0;
}
}发布于 2022-08-18 18:28:13
我可以在这里看到几个问题:
您的for循环初始化和更新counter,但对answer进行测试。这可以简化为简单循环,而计数器是< 10。
这个for循环也将计数器初始化为0,但这落在循环体的else中,它将打印win消息。
循环结束时返回0,使其在1次迭代后直接退出程序。
接下来,您没有在循环中更新您的answer (但我假设您以前有问题,所以您还没有做那么多)。您需要在内部调用scanf来更新答案。有很多需要改进的地方,但我认为这可以帮助你解决当前的问题。我尽量不改变你的代码。
for (int counter = 1; counter < 10; counter++) {
scanf_s("%c", &answer);
// you can handle the answer logic here
if (answer ...)
{
}
if (counter == 1)
{
printf("\n=========");
}
else if (counter == 2)
{
printf("\n+\n|\n|\n|\n|\n|\n=========");
}
else if (counter == 3)
{
printf("\n+---+\n| |\n|\n|\n|\n|\n=========");
}
else if (counter == 4)
{
printf("\n+---+\n| |\n| O\n|\n|\n|\n=========");
}
else if (counter == 5)
{
printf("\n+---+\n| |\n| O\n| |\n|\n|\n=========");
}
else if (counter == 6)
{
printf("\n+---+\n| |\n| O\n| |\n| / \\ \n|\n=========");
}
else if (counter == 7)
{
printf("\n+---+\n| |\n| O\n| /| \n| / \\ \n|\n=========");
}
else if (counter == 8)
{
printf("\n+---+\n| |\n| O\n| /|\\ \n| / \\ \n|\n=========");
}
else if (counter == 9)
{
printf("\nReally left me hanging there buddy");
break;
}
else
{
printf("\nThanks for saving me!");
break;
}
}
return 0;https://stackoverflow.com/questions/73407545
复制相似问题