Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-返回语句不退出void函数_C_Return - Fatal编程技术网

C-返回语句不退出void函数

C-返回语句不退出void函数,c,return,C,Return,我正在用C制作一个简单的connect four游戏,以加强我在C中所学的内容。我在多个地方读到,void函数中的return语句应该在那里结束函数的执行。但是,在下面的代码中,将数字放入第6行的一个数组后,它还会将数字放入第5行的同一列 void putInBottomRow(int col, int player) { if(row6[col] == 0) { row6[col] = player; return; } if(row5[col] == 0) { row

我正在用C制作一个简单的connect four游戏,以加强我在C中所学的内容。我在多个地方读到,void函数中的return语句应该在那里结束函数的执行。但是,在下面的代码中,将数字放入第6行的一个数组后,它还会将数字放入第5行的同一列

void putInBottomRow(int col, int player) {
if(row6[col] == 0) {
    row6[col] = player;
    return;
}
if(row5[col] == 0) {
    row5[col] = player;
    return;
}

return;
}
当我运行此命令时,这是输出: . 我错过什么了吗?这个问题可能在我的代码中的其他地方吗?谢谢

以下是其他相关代码:

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

int row1[] = {0, 0, 0, 0, 0, 0, 0};
int row2[] = {0, 0, 0, 0, 0, 0, 0};
int row3[] = {0, 0, 0, 0, 0, 0, 0};
int row4[] = {0, 0, 0, 0, 0, 0, 0};
int row5[] = {0, 0, 0, 0, 0, 0, 0};
int row6[] = {0, 0, 0, 0, 0, 0, 0};

/*
  Use row six as bottom row and row 1 as top row.
*/

int main()
{
printf("\n   Connect Four\n");
printGameScreen();
printf("Player 1's turn.");
playerPlace(1);
printGameScreen();
printf("Player 2's turn.");
playerPlace(2);
printGameScreen();
return 0;
}

void playerPlace(int player) {
int rowChoice;
printf("Player 1's turn!\nWhich column (1-7)? ");
scanf(" %d", &rowChoice);
rowChoice--;
if(rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
    return;
 } else {
    switch(rowChoice) {
    case 0 :
        putInBottomRow(rowChoice, player);
    case 1 :
        putInBottomRow(rowChoice, player);
    case 2 :
        putInBottomRow(rowChoice, player);
    case 3 :
        putInBottomRow(rowChoice, player);
    case 4 :
        putInBottomRow(rowChoice, player);
    case 5 :
        putInBottomRow(rowChoice, player);
    case 6 :
        putInBottomRow(rowChoice, player);
    }

}
return;
}
#包括
#包括
int row1[]={0,0,0,0,0,0,0};
int row2[]={0,0,0,0,0,0,0};
int row3[]={0,0,0,0,0,0,0};
int row4[]={0,0,0,0,0,0,0};
int row5[]={0,0,0,0,0,0,0};
int row6[]={0,0,0,0,0,0,0};
/*
使用第六行作为底行,第1行作为顶行。
*/
int main()
{
printf(“\n连接四个\n”);
printGameScreen();
printf(“玩家1的回合”);
playerPlace(1);
printGameScreen();
printf(“玩家2的回合”);
playerPlace(2);
printGameScreen();
返回0;
}
虚空玩家位置(智力玩家){
内行选择;
printf(“玩家1的回合!\n哪个栏(1-7)?”;
scanf(“%d”和&rowChoice);
选择--;
如果(rowChoice>6 | | rowChoice<0){
printf(“这不是一行。请再试一次。”);
playerPlace(玩家);
返回;
}否则{
开关(行选择){
案例0:
putInBottomRow(rowChoice,玩家);
案例1:
putInBottomRow(rowChoice,玩家);
案例2:
putInBottomRow(rowChoice,玩家);
案例3:
putInBottomRow(rowChoice,玩家);
案例4:
putInBottomRow(rowChoice,玩家);
案例5:
putInBottomRow(rowChoice,玩家);
案例6:
putInBottomRow(rowChoice,玩家);
}
}
返回;
}
您有:

if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
    return;
} else {
    switch (rowChoice) {
    case 0 :
        putInBottomRow(rowChoice, player);
    case 1 :
        putInBottomRow(rowChoice, player);
    case 2 :
        putInBottomRow(rowChoice, player);
    case 3 :
        putInBottomRow(rowChoice, player);
    case 4 :
        putInBottomRow(rowChoice, player);
    case 5 :
        putInBottomRow(rowChoice, player);
    case 6 :
        putInBottomRow(rowChoice, player);
    }
}
但进一步的研究表明,无论选择哪种方法,您都在进行相同的函数调用。您已经检测并报告了超出范围的值(这很好),因此整个开关应该成为一个简单的函数调用,因此您只需要:

if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
} else {
    putInBottomRow(rowChoice, player);
}
if(rowChoice>6 | | rowChoice<0){
printf(“这不是一行。请再试一次。”);
playerPlace(玩家);
}否则{
putInBottomRow(rowChoice,玩家);
}
请注意,
if
正文中的
返回
else
正文后面的语句也是
return
。事实上,
return
也不是必需的,因为函数不返回任何值,所以从函数底部掉下来也没有问题


在使用
行选项之前,应检查
scanf()
代码是否成功。您应该始终检查输入是否成功。

第6行和第5行的定义在哪里?它们的值是什么?当您使用逐步调试程序时会发生什么?否则问题可能会被解决。如果看不到更多代码,我们就无能为力了。可能您忘记了在执行
案例5
后的代码后,下一条语句就是下一条语句(例如,调用
putInBottomRow
some more`,它不会跳到switch语句的末尾。就是这样,谢谢!我知道switch在那里真是太过分了,但我只是想确保我能做所有事情,因为我刚学了C。这也是件好事,因为我不知道如何做switch语句哈哈。
if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
} else {
    putInBottomRow(rowChoice, player);
}