【问题标题】:Printf is not displaying a stringPrintf 不显示字符串
【发布时间】:2015-02-19 09:05:53
【问题描述】:

所以,我在这里遇到了一个小错误。为什么 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 我把main函数放在这里(不能贴出整个代码,因为它太大了),让你们感受一下是怎么回事。

#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;

}

【问题讨论】:

标签: c


【解决方案1】:

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

如果您的第一个 if 为真,则 else 关键字将不起作用 但是,它应该在循环的第二轮工作,当count != 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多