printf不打印

时间:2015-07-24 21:54:44

标签: c char printf

我有一些代码用于'nim'游戏的浅版本以便了解c。这部分是关于打印棋盘游戏,预期输出:

---------------
Box 1: ooo
Box 2: oooo
---------------

每个框中的“o”数由用户输入并作为参数发送给函数。

代码:

#include <stdio.h>
#define NUMBER_OF_BOXES 2
void printBoard(int number_of_balls_1, int number_of_balls_2)
{
   int boxes[NUMBER_OF_BOXES] = {number_of_balls_1, number_of_balls_2};
   printf("---------------\n");
   printBoxes(boxes);
   printf("---------------\n");
}

void printBoxes(int boxes[NUMBER_OF_BOXES])
{
    char ball = 'o';
    for(int i = 0; i < NUMBER_OF_BOXES; i++)
    {
        printf("Box %d: ", i+1);
        for(int j = 0; j < boxes[i]; ++j)
        {
            printf("%c", ball);
        }
        printf("\n");
    }
}

用于工作(三个小时前),我无法弄清楚出了什么问题!现在它给我的全部是:

"---------------
Box 0: Box 1: ---------------"

帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

#include <stdio.h>

#define NUMBER_OF_BOXES 2

void printBoxes(const int boxes[NUMBER_OF_BOXES])
{
    char ball = 'o';
    for(int i = 1; i <= NUMBER_OF_BOXES; i++)
    {
        printf("Box %d: ", i);
        for(int j = 0; j < boxes[i-1]; ++j)
        {
            printf("%c", ball);
        }
        printf("\n");
    }
}

void printBoard(const int number_of_balls_1, const int number_of_balls_2)
{
    int boxes[NUMBER_OF_BOXES]= {number_of_balls_1, number_of_balls_2};
    printf("---------------\n");
    printBoxes(boxes);
    printf("---------------\n");
}

答案 1 :(得分:0)

#include <stdio.h>
#define NUMBER_OF_BOXES 2

void printBoxes(const int boxes[NUMBER_OF_BOXES]) {
    char ball = 'o';
    for(int i = 0; i < NUMBER_OF_BOXES; i++) {
        printf("Box %d: ", i+1);
        for(int j = 0; j < boxes[i]; ++j) {
            printf("%c", ball);
        }
        printf("\n");
    }
}

void printBoard(const int number_of_balls_1, const int number_of_balls_2) {
        int boxes[NUMBER_OF_BOXES]= {number_of_balls_1, number_of_balls_2};
        printf("---------------\n");
        printBoxes(boxes);
        printf("---------------\n");
    }

void main() {
    printBoard(3, 4);
}

编译:

gcc -std=c99 box.c -o box

输出:

---------------
Box 1: ooo
Box 2: oooo
---------------