【问题标题】:Dice Game Numbers not Random骰子游戏号码不是随机的
【发布时间】:2015-06-21 11:21:05
【问题描述】:

基本上我应该模拟一个掷三个骰子的程序,把它加起来。然后我会让用户猜测下一个滚动是更高、更低、相同还是他们只是想退出。我有两个问题。

  1. 这些数字不是随机的,显然这是我的一个大错误,但我似乎无法弄清楚。我在想我不需要第二个包含 3 个骰子对吗?反正他们一点帮助都没有。

  2. 无论如何,我的程序会同时执行所有 if/else if 语句。显然我不希望这种情况发生。


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
    int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice; 
    int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;

    srand( time(NULL) );

    diceOne      = rand() % 6 + 1;
    diceTwo      = rand() % 6 + 1;
    diceThree    = rand() % 6 + 1;
    newDiceOne   = rand() % 6 + 1;
    newDiceTwo   = rand() % 6 + 1;
    newDiceThree = rand() % 6 + 1;

    printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
    diceSum = diceOne + diceTwo + diceThree;
    printf("\nTotal sum of dice is %d\n", diceSum);

    do {
        printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
        printf(" You have been correct %d times\n", timesCorrect);
        scanf("%d", &choice);

        printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
        newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
        printf("\nTotal sum of dice is %d\n", newDiceSum);

        if (choice == 1)
        {
            if (newDiceSum > diceSum);
                timesCorrect++;
            printf("You are correct!\n");
        }
        else if (newDiceSum < diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 2)
        {
            if (newDiceSum < diceSum);
                timesCorrect++;
            printf("You are correct!\n");
        }
        else if (newDiceSum > diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 3)
        {
            if (newDiceSum == diceSum);
                timesCorrect ++;
            printf("You are correct!\n");
        }
        else if (newDiceSum != diceSum); 
        {
            printf("You are incorrect, sorry!\n");
        }

        if (choice == 4)
        {
            printf("Thanks for playing!!!!!!\n");
            system("pause");

            return 0;
        }
    } while (choice!= 4 );
}

【问题讨论】:

  • 请缩进你的代码,不要学习任何使用system("pause");的东西,如果它在你的教科书中,那么请放弃它,如果你的老师使用它,那就换你的老师。跨度>
  • 你在哪里掷骰子?不在循环中
  • 请注意,数字不会均匀分布,因为rand() 很可能会返回模 2**n 的值。
  • 我觉得练习就够了
  • 为了清晰和简单的文档以及让我们人类更容易阅读代码,每个语句只声明一个变量。

标签: c random


【解决方案1】:

else if 条件句后多了一个分号,就像这里

else if (newDiceSum < diceSum); 
                /*            ^ this should not be here */

如果您使用具有良好诊断功能并启用警告的编译器,它应该警告您“typo”,如果您想将块留空,请使用大括号,如

else if (newDiceSum < diceSum) {}

此外,您使用rand() 设置第一个骰子,它们是随机值,但您始终在循环中使用相同的值。

【讨论】:

  • 缩进会有帮助
  • 谢谢。我知道这一点,所以我不确定我为什么要把分号放在那里。但是,它仍然会通过所有 3 例如你是对的!你错了,对不起你错了,对不起。
【解决方案2】:

以下代码:

  1. 处理错误情况

  2. 消除不需要的变量

  3. 干净编译

  4. 执行成功


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ( void )
{
    int diceOne;
    int diceTwo;
    int diceThree;
    int diceSum=0;
    int timesCorrect=0;
    int choice;

    int newDiceSum;

    srand( time(NULL) );

    diceOne      = rand() % 6 + 1;
    diceTwo      = rand() % 6 + 1;
    diceThree    = rand() % 6 + 1;


    printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
    diceSum = diceOne + diceTwo + diceThree;
    printf("\nTotal sum of dice is %d\n", diceSum);

    do {
        printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
        printf(" You have been correct %d times\n", timesCorrect);
        if( 1 != scanf("%d", &choice) )
        { // then scanf failed
            perror( "scanf for choice failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, scanf successful

        diceOne      = rand() % 6 + 1;
        diceTwo      = rand() % 6 + 1;
        diceThree    = rand() % 6 + 1;

        printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
        newDiceSum = diceOne + diceTwo + diceThree;
        printf("\nTotal sum of dice is %d\n", newDiceSum);

        switch( choice )
        {
        case 1:
            if (newDiceSum > diceSum)
            {
                timesCorrect++;
                printf("You are correct!\n");
            }
            else
            {
                printf("You are incorrect, sorry!\n");
            }
            break;

        case 2:
            if (newDiceSum < diceSum)
            {
                timesCorrect++;
                printf("You are correct!\n");
            }
            else
            {
                printf("You are incorrect, sorry!\n");
            }
            break;

        case 3:
            if (newDiceSum == diceSum)
            {
                timesCorrect ++;
                printf("You are correct!\n");
            }
            else
            {
                printf("You are incorrect, sorry!\n");
            }
            break;

        case 4:
            printf("Thanks for playing!!!!!!\n");
            system("pause");
            break;

        default:
            printf( " invalid choice, valid choices are 1...4\n");
            break;
        } // end switch
    } while (choice!= 4 );
    return(0);
} // end function: main

【讨论】:

  • 最初我赞成你的回答,但后来我注意到你没有解释为什么 OP 代码失败了,returns (0) 是什么?您正在使用它,就好像它是一个函数一样!
  • 谢谢!请问diceOne = rand() % 6 + 1;骰子二 = rand() % 6 + 1;骰子三 = rand() % 6 + 1;在循环中使它工作?我的意思是它工作得很好,但我想从我的错误中吸取教训,而不是仅仅让它发挥作用而不从我的错误中吸取教训。
猜你喜欢
  • 1970-01-01
  • 2016-05-07
  • 2011-07-18
  • 1970-01-01
  • 2021-08-05
  • 2016-03-22
  • 2014-02-28
  • 2014-10-07
  • 2012-02-29
相关资源
最近更新 更多