选择3个最大的数字并将它们加在一起

时间:2015-03-13 01:05:26

标签: c

我认为我的逻辑是合理的...但我想要做的是当有人选择4个骰子时,它需要最多3个数字并将它们加在一起。下面的小部分随机选择4个骰子。

请参阅下面的代码(原谅不好的评论)

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





int Roll_Dice(void)
{
setvbuf(stdout, 0, _IONBF, 0);
int dice1 = 0;
int dice2 = 0;
int dice3 = 0;
int dice4 = 0;
int dice_roll= 0;
int sides;
int dice;
int min;

srand(time(NULL));
{
rollagain:   
printf("how many sides of the dice are there? (maximum 12)");
scanf ("%d", &sides);

choosedice:
printf("how many dice are there? (maximum 4)\n");
scanf ("%d", &dice);

if (sides > 12)
    {
        printf("this is not a valid input, must be 12 or less\n ");

        goto rollagain;
       } else { 
            if (dice == 1){
    dice1 = (rand() % sides) + 1;
    dice_roll = dice1;
} else {
    if (dice == 2){
            dice1 = (rand() % sides) + 1;
            dice2 = (rand() % sides) + 1;
            dice_roll = dice1 + dice2;
        } else {
        if (dice == 3){
            dice1 = (rand() % sides) + 1;
            dice2 = (rand() % sides) + 1;
            dice3 = (rand() % sides) + 1;
            dice_roll = dice1 + dice2 + dice3;
            } else {
                if (dice == 4){
            dice1 = (rand() % sides) + 1;
            dice2 = (rand() % sides) + 1;
            dice3 = (rand() % sides) + 1;
            dice4 = (rand() % sides) + 1;
                } /* dice_roll = dice1 + dice2 + dice3 + dice4; */
            min = dice1;
            if ((dice2 < min)){ //this is the section where it should choose the highest 3 numbers. but i think i have nested it incorrectly...
                min == dice2;
                         {
                            if (dice3 < min)
                            min == dice3;
                        else {
                            if (dice4 < min)
                            min == dice4;
                        }
                        }
                dice_roll = dice1 + dice2 + dice3 + dice4 - min;


            } else {
                    if (dice > 4) {
                        printf ("The amount of dice must be 4 or Less, try Again\n");
                        goto choosedice;
                    }

            }

            }
        }
}
printf("the number you rolled was %d \n", dice_roll);
return dice_roll;
}
}

}

我认为我错误地嵌套它......或者把它放在错误的位置。就像我说的,我认为我对我如何做的逻辑是可以的,但它找到了正确的地方。有人能发现什么吗?

我确实意识到我使用了if语句,然后在那个语句中使用了另一个if语句......但是我无法用那里的else语句来完成它。

将代码用于在其自己的函数中选择最高的3个数字,然后在if语句中调用该函数是否也可行?

感谢您的意见。

1 个答案:

答案 0 :(得分:3)

您可以使用简单的if循环替换所有嵌套的for语句。

int rollDice( void )
{
    int sides = getNumber( "how many sides of the dice are there? (maximum 12)", 12 );
    int dice  = getNumber( "how many dice are there? (maximum 4)", 4 );

    int min = 1000;
    int roll = 0;
    for ( int i = 0; i < dice; i++ )
    {
        int value = rand() % sides + 1;
        if ( value < min )
            min = value;
        roll += value;
    }
    if ( dice == 4 )
        roll -= min;

    return( roll );
}

用户输入可以在子程序中处理,在用户输入有效数字之前不会返回。

int getNumber( char *prompt, int max )
{
    int result = 0;
    for (;;)
    {
        printf( "%s ", prompt );
        fflush( stdout );

        if ( scanf( "%d", &result ) != 1 )
            exit( 1 );

        if ( result >= 1 && result <= max )
            break;

        printf( "this is not a valid input, must be %d or less\n", max );
    }
    return( result );
}

srand只应调用一次,通常在main

的开头
int main( void )
{
    srand(time(NULL));
    for (;;)
    {
        int roll = rollDice();
        printf( "the number you rolled was %d\n", roll );
    }
}