1

我正在尝试学习 C 语言,在这里我需要一些帮助。

我正在尝试为井字游戏制作程序。我尝试在 main 函数中执行它的第一部分,但它不起作用。但是,当我使用另一个功能时,它起作用了。它是如何以及为什么起作用的?(这两个程序都附在下面)。

另外,我在带有“square 1”和“square 2”的scanf中使用了“%s”而不是“%c”。为什么“%c”在这里不能正常工作?

在此先感谢您的帮助。

// first program


#include <stdio.h>  

int main ()

{

int player;                                         
char square1; 
char square2;
char Nesta[9]={'1','2','3','4','5','6','7','8','9'}; 
int player1;
int player2;


printf("choose one for x or two for o\n");  


scanf("%i", &player); 

if (player==1) 
{
    player1='x';
    player2='o';
}
else
{
    player1 ='o';
    player2='x';
}


printf(
       "1  +  2 + 3   \n"
       "---+----+-----\n"
       "4  +  5 + 6   \n"
       "---+----+-----\n"
       "7  +  8 + 9   \n"
       );


for (int i=0; i<3; ++i)
{
    printf("please enter the number of the sqaure "); 
    scanf("%s",&square1 );

    printf("please enter the number of the sqaure "); 
    scanf("%s",&square2 );

    for (int j=0; j<9; ++j)
    {
        if (square1 == Nesta[j])

        {
            Nesta[j]=player1;
            printf(
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
        }
    }


    for (int k=0; k<9; ++k)
    {
        if (square2 == Nesta[k])

        {
            Nesta[k]=player2;
            printf(
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],
                                         Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
         }

       }
     }
      return 0; 
   }

使用 gamecont 功能时它工作得很好!

// the second program
#include <stdio.h> 

char Nesta[9]={'1','2','3','4','5','6','7','8','9'}; 
int player1;
int player2;

int gamecont ()
{

char square2;

printf("please enter the number of the square "); 
scanf("%s",&square2 ); 

for (int j=0; j<9; ++j)

    if (square2 == Nesta[j])
      {
        Nesta[j]=player2;
        printf(
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],
                                    Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
        }
    }


int main ()
{

int player;                                         
char square1; 

printf("choose one for x or two for o\n");  

scanf("%i", &player); 

if (player==1) 
{
    player1='x';
    player2='o';
}
else
{
    player1 ='o';
    player2='x';
}


printf(
       "1  +  2 + 3   \n"
       "---+----+-----\n"
       "4  +  5 + 6   \n"
       "---+----+-----\n"
       "7  +  8 + 9   \n"
       );



 for (int i=0; i<3; ++i)
 {
 printf("please enter the number of the square  "); 
 scanf("%s",&square1 ); 

 for (int j=0; j<9; ++j)
 {
   if (square1 == Nesta[j])
    {  
        Nesta[j]=player1;

        printf(
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],Nesta[5],Nesta[6],Nesta[7],Nesta[8]);

gamecont() ;   
            }
}
  }
 return 0;
}
4

2 回答 2

2

当你说某事“不起作用”时,你需要清楚。在什么意义上不起作用?电脑坏了?或者发生了一些你没有预料到的具体事情?然后具体一点。

WRT %cvs %s,请记住,当您从控制台输入数据时,您需要点击“enter”,这会将 '\n' 字符提供给 stdin 缓冲区。 '\n' 仍在输入缓冲区中,等待下一个读取scanf

使用 时%s,如果缓冲区中有换行符,则会跳过该换行符,因为%s字符串不包含空格,并且大多数类型说明符都会跳过前导空格。但是,%c始终是单个字符。考虑:

char c;
while (scanf("%c", &c)) printf("%d\n", c);

每次你输入一个字符(然后按回车键)你会得到两个数字,其中第二个是 10(ascii \n)。

不幸的是,您对 %s 的使用是不安全的。试试这个:

scanf("%c%*c", &square);

意味着忽略,因此这*将删除换行符,使标准输入缓冲区为空。但是,如果用户输入了多个字母(或空格),仍然会留下一些东西。您可以创建square一个字符串并只使用第一个字符,但这也有缺陷。刷新输入缓冲区的一种可靠方法是:

while (fgetc(stdin) != '\n');

如果您在使用 scanf 后这样做,它会在您捕获后吃掉缓冲区中的任何内容,并在下一次时将其清除。

于 2012-05-28T10:00:15.793 回答
0

在第一个代码转储中,您将带有 %s 的 c 字符串扫描到字符变量(square1 和 square2)。这是不安全的,您应该将 c 字符串扫描到字符数组,而不是单个字符。

于 2012-05-28T09:58:24.390 回答