gpt4 book ai didi

c - Printf 不显示字符串

转载 作者:行者123 更新时间:2023-11-30 21:39:48 25 4
gpt4 key购买 nike

所以,我在这里遇到了一个小错误。为什么 printf 不显示第二个字符串?

printf("Player 1: ");          //asks the players names
fgets(name1, 30, stdin);
printf("Player 2: ");
fgets(name2, 30, stdin);
fulfil(table);
player=choose_player();
shows(table);
int i;
for (i=0, count=player; i<9; i++)
{
if (count==1)
printf("It's your turn, %s \n", name1);
else
printf("It's your turn, %s \n", name2);

(..) 输出:

玩家 1:大卫·洛佩斯

玩家 2:鲁本·阿莫林(...)

轮到你了,大卫·洛佩斯

轮到你了,__________

为什么不显示第二个名字?

谢谢!

编辑:

由于我是编程新手,也是这个论坛的新手,所以不要对我抱太大期望! :S 我把主要函数留在这里(不能发布整个代码,因为它太大了),以便你们可以感受一下发生了什么。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define PLAYER1 88 //ASCII tabble 88='X'
#define PLAYER2 79 //ASCII table 79='O'

void intro();
int read ();
void fulfill(int v[]);
int control (int num1, int table[]);
int control_victory(int line[]);
int choose_player();
int show(int table[]);

int main (void)
{
char name1[30], name2[30];
int table[8], num, w, player, contador, k;
intro();
printf("Player 1: "); //Asks the names
fgets(name1, 30, stdin);
printf("Player 2: ");
fgets(name2, 30, stdin);
fulfill(table);
player=choose_player();
show(table);
int i;
for (i=0, contador=player; i<9; i++)
{
if (contador==1)
printf("It's your turn, %s \n", name1);
else if (contador==2)
printf("It's your turn, %s \n", name2);
w=read();
if (w==(-1)) //if the user enters '0', the program will exit
break;
if (contador==1)
{
int verf;
k=control (w, table); //verifies if the number inserted is valid
table[k]=PLAYER1; //Inserts the char on the array
show(table); //presents a new table
verf=control_victory(table); //verifies if there is victory for the player 1
if (verf==1) //
{
printf("\n%s won the game!\n", name1);
return 0;
}
else if (verf==3)
printf("\n Draw!\n");
contador++;
}
else if (contador==2) //Same here applies for player 2
{
int verf;
k=control (w, table);
table[k]=PLAYER2;
show(table);
verf=control_victory(table);
if (verf==2)
{
printf("\n%s won the game!\n", name2);
return 0;
}
else if (verf==3)
printf("\n Draw!\n");

contador--;
}
}
return 0;

}

最佳答案

您的程序只能同时显示两个名称之一。

如果您的第一个 if 为 true,则 else 关键字将不起作用但是,当 count != 1

时,它应该在循环的第二轮起作用。

关于c - Printf 不显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com