所以每当我尝试运行这段代码时,它都会通过第一个printf,但它会显示第二个printf,而不让我输入值。我做错了什么吗?
#include <stdio.h>
int main(int argc, const char * argv[])
{
int suit1;
int suit2;
char H, S, C, D;
float value1;
float value2;
printf("Please enter the card's suit");
scanf("%d", &suit1);
printf("Please enter the card's value");
scanf("%f", &value1);
printf("%d %f", suit1, value1);
}
考虑使用字符("CDHS")作为花色,使用整数作为牌(而不是浮点数 - 尽管它们可以表示小整数而不会损失精度)。当可以的话,使变量的内部表示"接近现实世界"通常是一个好主意......
通过一个小的修改,你的程序对我来说很好用(根据你最近的评论更新):
#include <stdio.h>
int main(int argc, const char * argv[])
{
int suit1;
int suit2;
char suitInput;
char Hearts, Spades, Clubs, Diamonds;
int value1;
int value2;
printf("Please enter the card's suit (C, D, H or S): ");
scanf("%c", &suitInput);
printf("nPlease enter the card's value: (1 = Ace, up to 13 = King): ");
scanf("%d", &value1);
printf("nYou entered %c, %dn", suitInput, value1);
}
我得到的输出:
Please enter the card's suit (C, D, H or S): D
Please enter the card's value: (1 = Ace, up to 13 = King): 5
You entered D, 5
我怀疑您正在为第一个输入输入字母或字符串,这将导致"跳过"第二个输入(因为第一次扫描不会消耗字母,第二次扫描会失败)。
将西装扫描为字符或字符串:
char *suit;
suit = malloc(100 * sizeof(char));
scanf("%sn", suit);
或输入两个数字