我想创建一个骰子游戏,但它总是说我的猜测是错误的,并且为什么这么说。 我终于明白了如何使rand函数实际上是随机的btw:D 我认为这是因为数组没有正确存储字符串。
int main()
{
srand(time(NULL));
int dice1 = 1, dice2 = 1, dice3 = 1, sum, key = 0;
int dice4 = 1, dice5 = 1, dice6 = 1, sum2;
char randomnumber[10];
printf("Diceroll-game \n\n");
printf("The first sum is:\n");
dice1 = (rand()%6 + 1);
dice2 = (rand()%6 + 1);
dice3 = (rand()%6 + 1);
sum = dice1 + dice2 + dice3;
printf("%d\n", sum);
printf("Will the next sum of 3 dices be higher(hi), lower (lo) or the same(sa) (hi, lo, sa)?\n");
printf("Type hi, lo or sa\n");
scanf("%c \n", &randomnumber);
dice4 = (rand()%6 + 1);
dice5 = (rand()%6 + 1);
dice6 = (rand()%6 + 1);
sum2 = dice4 + dice5 + dice6;
if (randomnumber == "hi" && sum2 > sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (randomnumber == "lo" && sum2 < sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (randomnumber == "sa" && sum2 == sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
printf("\nIt's %d", sum2);
return 0;
是的,请有人帮帮我。
谢谢。
答案 0 :(得分:2)
首先,您在这里使用了错误的格式说明符。 %c
用于扫描一个char
。您需要将%s
用于字符串。使用错误的格式说明符调用undefined behaviour。
然后,您提供给scanf()
的格式字符串需要与输入完全匹配。你在那里不需要newline
。
此外,randomnumber
是一个数组,在使用&
参数时添加scanf()
不是必需的。
更改
scanf("%c \n", &randomnumber);
到
scanf("%s", randomnumber);
其次,无法使用==
运算符完成字符串comaprisons。您需要使用strcmp()
来比较两个字符串内容。
更改
.. randomnumber == "hi" ...
到
.. !strcmp(randomnumber, "hi") ...
答案 1 :(得分:2)
三个问题:
您使用错误的格式说明符来扫描字符串。 %c
用于char
,%s
用于字符串。所以改变
scanf("%c \n", &randomnumber);
到
scanf("%s", randomnumber);
我做的其他更改是删除\n
和空格字符,因为它不会停止扫描,直到非空格字符,这可能不是您想要的。此外,%s
还需要char*
。 &randomnumber
类型为char(*)[10]
,而randomnumber
转换为&randomnumber[0]
,char*
,这正是%s
想要拥有的内容。
在C中,当您使用==
比较字符串时,您需要比较指针而不是实际内容。要比较内容,请添加string.h
并使用strcmp
。如果两个字符串相等,则返回0。所以改变以下内容:
if (randomnumber == "hi" && sum2 > sum) {
if (randomnumber == "lo" && sum2 < sum) {
if (randomnumber == "sa" && sum2 == sum) {
到
if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
if (strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
if (strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
现在,在你解决了其他问题之后,你会注意到有些东西打印了两次,这可能不是你想要的。原因是您有if
个strcmp
个"You were right !\n"
。只有一个可以是真实的,将打印"You were wrong."
,其余的将打印if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
。通过更改
if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
printf("You were right !\n\a");
}else if(strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
printf("You were right !\n\a");
}else if(strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
到
key
请注意, img.onload = function(callback){
变量在您的程序中是多余的。