C diceroll游戏

时间:2015-06-10 15:06:12

标签: c windows dice

我想创建一个骰子游戏,但它总是说我的猜测是错误的,并且为什么这么说。 我终于明白了如何使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;
是的,请有人帮帮我。 谢谢。

2 个答案:

答案 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)

三个问题:

  1. 您使用错误的格式说明符来扫描字符串。 %c用于char%s用于字符串。所以改变

    scanf("%c \n", &randomnumber);
    

    scanf("%s", randomnumber);
    

    我做的其他更改是删除\n和空格字符,因为它不会停止扫描,直到非空格字符,这可能不是您想要的。此外,%s还需要char*&randomnumber类型为char(*)[10],而randomnumber转换为&randomnumber[0]char*,这正是%s想要拥有的内容。

  2. 在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) {
    
  3. 现在,在你解决了其他问题之后,你会注意到有些东西打印了两次,这可能不是你想要的。原因是您有ifstrcmp"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
  4. 请注意, img.onload = function(callback){ 变量在您的程序中是多余的。