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次... 我该怎么办?
答案 0 :(得分: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))
试试。
答案 1 :(得分: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);
这样每个人都知道一个号码何时结束而另一个号码何时开始。