为什么我的 for 循环没有在 C 中给出预期的输出?
Why does my for loop not give intended output in C?
我正在尝试向我在 for 循环中定义的数组中添加一个字母。每次用户输入内容时,我想将 "counterSoFar" 减 1,然后显示它。但是,"counterSoFar" 总是减 2 并跳过用户输入任何内容的机会,而不是将 "counterSoFar" 减 1。我已经尝试多次查看代码,但我仍然无法理解出为什么。请指教
# include <stdio.h>
# include <ctype.h>
void userInput(char[]);
int main() {
printf("Player 1 please enter a word of up to 12 letters.\n");
char word[13];
scanf("%13s", word);
userInput(word);
}
void userInput(char word[])
{
int n = strlen(word);
for (int i = 0; i < n; i++)
{
word[i] = tolower(word[i]);
}
int checker;
for (int i = 0; i < n; i++)
{
if (isalpha(word[i]))
{
checker = 1;
}
else
{
checker = 0;
printf("Please enter a word without spaces and numbers!\n");
break;
}
}
if (checker == 1)
{
int counterSoFar = 8;
char letter[1];
printf("The word to guess is as follows: \n");
for (int i = 0; i < n; i++)
{
printf("_");
}
int maxTries = 7;
for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
{
printf("\n");
counterSoFar = counterSoFar - 1;
printf("You have %d tries left!\n\n", counterSoFar);
printf("Player 2 please enter only 1 letter!\n");
scanf("%c", &letter);
char array[13];
for (int c = 0; c < n; c++)
{
if (letter == word[c])
{
array[c] = word[c];
}
else
{
array[c] = '_';
}
}
printf("The current array is %c", array);
}
}
}
您需要更改声明:
scanf("%c", letter);
至:
scanf(" %c", &letter);
因为scanf
需要一个指针作为参数,所以你应该传递变量的地址。 %c
说明符之前的 space 需要消耗输入之间的 \n
。
scanf("%c",&letter);
您还将获得 \n
作为另一个输入。所以使用 scanf(" %c",&letter);
来消耗 \n
。
此外,如果您考虑一些事情:-
maxTries
是您希望保留在 for
循环之外的内容。你可能会问为什么?但关键是它有助于保留程序以后可能需要的东西,并提高可读性。
其次,如果您使用的是 counterSoFar
,那么我猜它是由某些东西启动的。这再次支持我的第一点。您不需要在循环中重新初始化 maxTries
。
作为对您评论的回答,您做错了很多事情。我会一一尝试。
char word[13];
用于 12 个字符和一个空终止字符 [=20=]
。
scanf("%12s",word);
应该是char letter
而不是char letter[1]
。为什么要使用 1 个大小的字符数组而不是一个字符?
char array[13];
应该在 for
循环之外以保留已经猜到的字符。
你应该初始化它而不是字母字符来区分哪些正在填充哪些不是。
这是一个结合更正的小例子:-(如果你尝试在其中结合其他游戏逻辑会更好..我已经展示了最低限度的部分)。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void userInput(char[]);
int main() {
printf("Player 1 please enter a word of up to 12 letters.\n");
char word[13];
scanf("%12s", word);
userInput(word);
}
void userInput(char word[])
{
int n = strlen(word);
for (int i = 0; i < n; i++)
word[i] = tolower(word[i]);
char letter;
printf("The word to guess is as follows: \n");
for (int i = 0; i < n; i++)
printf("_ ");
char array[13]={0};
for(int i=0;i<=11;i++)
array[i]='_';
int maxTries = 16;
int matched = 0;
for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
{
printf("\n");
printf("You have %d tries left!\n\n", maxTries - guessCounter);
printf("Player 2 please enter only 1 letter!\n");
scanf(" %c", &letter);
for (int c = 0; c < n; c++)
{
if (letter == word[c] && array[c]=='_')
{
matched++;
array[c] = letter;
break;
}
}
if( matched == n){
printf("You won!!!!");
return;
}
printf("The current array is ");
for(int i=0;i<n;i++)
printf("%c ",array[i]);
}
printf("You lose!!!!");
}
改第一个正确的是
printf("Player 2 please enter only 1 letter!\n");
scanf("%c", &letter);
我正在尝试向我在 for 循环中定义的数组中添加一个字母。每次用户输入内容时,我想将 "counterSoFar" 减 1,然后显示它。但是,"counterSoFar" 总是减 2 并跳过用户输入任何内容的机会,而不是将 "counterSoFar" 减 1。我已经尝试多次查看代码,但我仍然无法理解出为什么。请指教
# include <stdio.h>
# include <ctype.h>
void userInput(char[]);
int main() {
printf("Player 1 please enter a word of up to 12 letters.\n");
char word[13];
scanf("%13s", word);
userInput(word);
}
void userInput(char word[])
{
int n = strlen(word);
for (int i = 0; i < n; i++)
{
word[i] = tolower(word[i]);
}
int checker;
for (int i = 0; i < n; i++)
{
if (isalpha(word[i]))
{
checker = 1;
}
else
{
checker = 0;
printf("Please enter a word without spaces and numbers!\n");
break;
}
}
if (checker == 1)
{
int counterSoFar = 8;
char letter[1];
printf("The word to guess is as follows: \n");
for (int i = 0; i < n; i++)
{
printf("_");
}
int maxTries = 7;
for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
{
printf("\n");
counterSoFar = counterSoFar - 1;
printf("You have %d tries left!\n\n", counterSoFar);
printf("Player 2 please enter only 1 letter!\n");
scanf("%c", &letter);
char array[13];
for (int c = 0; c < n; c++)
{
if (letter == word[c])
{
array[c] = word[c];
}
else
{
array[c] = '_';
}
}
printf("The current array is %c", array);
}
}
}
您需要更改声明:
scanf("%c", letter);
至:
scanf(" %c", &letter);
因为scanf
需要一个指针作为参数,所以你应该传递变量的地址。 %c
说明符之前的 space 需要消耗输入之间的 \n
。
scanf("%c",&letter);
您还将获得
\n
作为另一个输入。所以使用scanf(" %c",&letter);
来消耗\n
。
此外,如果您考虑一些事情:-
maxTries
是您希望保留在for
循环之外的内容。你可能会问为什么?但关键是它有助于保留程序以后可能需要的东西,并提高可读性。其次,如果您使用的是
counterSoFar
,那么我猜它是由某些东西启动的。这再次支持我的第一点。您不需要在循环中重新初始化maxTries
。
作为对您评论的回答,您做错了很多事情。我会一一尝试。
char word[13];
用于 12 个字符和一个空终止字符[=20=]
。
scanf("%12s",word);
应该是
char letter
而不是char letter[1]
。为什么要使用 1 个大小的字符数组而不是一个字符?char array[13];
应该在for
循环之外以保留已经猜到的字符。你应该初始化它而不是字母字符来区分哪些正在填充哪些不是。
这是一个结合更正的小例子:-(如果你尝试在其中结合其他游戏逻辑会更好..我已经展示了最低限度的部分)。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void userInput(char[]);
int main() {
printf("Player 1 please enter a word of up to 12 letters.\n");
char word[13];
scanf("%12s", word);
userInput(word);
}
void userInput(char word[])
{
int n = strlen(word);
for (int i = 0; i < n; i++)
word[i] = tolower(word[i]);
char letter;
printf("The word to guess is as follows: \n");
for (int i = 0; i < n; i++)
printf("_ ");
char array[13]={0};
for(int i=0;i<=11;i++)
array[i]='_';
int maxTries = 16;
int matched = 0;
for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
{
printf("\n");
printf("You have %d tries left!\n\n", maxTries - guessCounter);
printf("Player 2 please enter only 1 letter!\n");
scanf(" %c", &letter);
for (int c = 0; c < n; c++)
{
if (letter == word[c] && array[c]=='_')
{
matched++;
array[c] = letter;
break;
}
}
if( matched == n){
printf("You won!!!!");
return;
}
printf("The current array is ");
for(int i=0;i<n;i++)
printf("%c ",array[i]);
}
printf("You lose!!!!");
}
改第一个正确的是
printf("Player 2 please enter only 1 letter!\n");
scanf("%c", &letter);