【问题标题】:C diceroll gameC 骰子游戏
【发布时间】:2015-08-25 22:30:46
【问题描述】:

我想创建一个骰子游戏,但它总是说我的猜测是错误的,我不知道为什么。 我终于明白如何使 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;

谁能帮帮我。 谢谢。

【问题讨论】:

  • 我仍然有 key 变量,它现在说“你错了。你是对的!”
  • Bucky 创建了可怕的教程和练习。给自己一本书,伙计!附言rand伪随机数生成器,而不是真正的随机数生成器
  • @undefinedbehaviour ,“巴基”是谁?
  • @CoolGuy Bucky 似乎是this challenge 的起源,介于poor tutorials and undefined behaviour (e.g. buffer overflows) 之间...不要费心去指出他的任何问题;他创造的任何东西都不会有问题;)

标签: c windows dice


【解决方案1】:

三个问题:

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

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

    scanf("%s", randomnumber);
    

    我所做的其他更改是删除 \n 和空格字符,因为它不会停止扫描,直到出现非空白字符,这可能不是您想要的。此外,%s 需要 char*&amp;randomnumberchar(*)[10] 类型,而 randomnumber 被转换为 &amp;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. 现在,在您解决了其他问题之后,您会注意到有些内容会打印两次,这可能不是您想要的。原因是您有三个ifs 和strcmps。最多只有一个为真,它将打印"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 变量在您的程序中是多余的。

【讨论】:

  • 第二点非常正确。我停在第一点本身。很好的收获,+1。
  • @DavisR5 ,检查已编辑答案中的问题编号 3。
【解决方案2】:
  • 首先,您在这里使用了错误的格式说明符。 %c 用于扫描一个char。您需要将%s 用于字符串。使用错误的格式说明符会调用undefined behaviour

    然后,您提供给scanf()格式字符串 需要与输入完全匹配。你不需要newline

    另外,randomnumber 是一个数组,不需要添加 &amp; 作为 scanf() 参数。

    改变

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

    scanf("%s", randomnumber);
    
  • 其次,不能使用== 操作符来完成字符串匹配。您需要使用strcmp() 来比较两个字符串内容。

    改变

    .. randomnumber == "hi" ...
    

    ..  !strcmp(randomnumber, "hi")   ...
    

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 2013-10-14
    • 2012-11-27
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2021-12-14
    相关资源
    最近更新 更多