【发布时间】:2013-10-22 12:07:50
【问题描述】:
我是 C 编程新手,我的任务是创建一个简单的井字游戏。
我设法用数组和循环创建了一个空板。现在我需要获取用户的输入,并将“X”/“O”放入板中。
代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Declare the variables
//Create the board
//Initialize the board with all blanks
//Print to screen
//Prompt user for letter 'X', 'O' or 'q' to quit
//declare the variables
char board[3][4], input;
int rows, columns;
do
{
//create the board
for ( rows = 0 ; rows <3 ; rows++ )
{
for ( columns = 0 ; columns < 4 ; columns++ )
{
//Initialize array to blanks (' ')
board[rows][columns] = '|';
//print to screen
printf( "%c\t", board[rows][columns] );
}
printf("\n\n\n");
}
printf( "Hit X or O. 'q' to quit\n\n" );
scanf("%c", &input);
} while ( input != 'q' );
}//end main
任务说我可以使用此代码fflush(stdin) 来清除键盘缓冲区,我显然不知道它是什么以及如何使用它:(
我对该代码进行了一些研究,它似乎取代了现有的输入和输出。如果我错了,请纠正我。
那么在我目前的情况下如何使用fflush(stdin)?
山姆
【问题讨论】:
-
如你所说清除键盘缓冲区,请将其放在你的scanf之前,输入输入前需要清除缓冲区
标签: arrays loops tic-tac-toe fflush