0
while((player1chip==-1)||(player2chip==-1)||(player3chip==-1)){                                                              
    player1chip=-1; player2chip=-1; player3chip=-1;                                                       
    printf("How many chips would you like to play with (3-10):\n");
    scanf("%d%d%d", &player1chip, &player2chip, &player3chip);
    getchar();
}

我想一次获得 3 个值,但循环似乎已经运行了 3 次......我应该怎么做?

4

2 回答 2

0

scanf() 语句对于您要执行的操作有点奇怪,因为您说您期望使用“%d%d%d”来没有分隔的三个数字。如果 while 循环的目的是填充 playerchip 变量,则应为它们使用一个数组并遍历该数组,如下所示:

playerchip[3];   
for(int i=0; i<3; i++) {
    printf("How many chips would player %d like to play with (3-10)?:\n", i);
    scanf("%d", &playerchip[i]);
}

如果保留命名方案,则不能使用循环轻松填充变量。不过,要回答您最初的问题,要一次获取多个数字,您需要在 scanf() 语句中添加一些分隔符:

scanf("%d %d %d", &a, &b, &c);

这样,每个人都知道一个数字何时结束,另一个数字何时开始。

于 2012-09-27T05:14:42.913 回答
0
int p1chip=-1, p2chip=-1, p3chip=-1;
do {
  printf("How many chips would you like to play with (3-10)?\n");
  scanf("%d%d%d", &p1chip, &p2chip, &p3chip);
} while ((p1chip == -1) || (p2chip == -1) || (p3chip == -1))

试试看。

于 2012-09-27T01:37:31.907 回答