微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

掷两个骰子并呈现总数,然后要求用户猜测下一个总数

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

int main()
{
    int dice1, dice2;
    int total1, total2;
    time_t t;
    char ans;

    srand(time(&t));

    // give you a number between 0 and 5, so the + 1 makes it 1 to 6

    dice1 = (rand() % 5) + 1;
    dice2 = (rand() % 5) + 1;
    total1 = dice1 + dice2;

    printf(First roll of the dice was %d and %d, , dice1, dice2);
    printf(for a total of %d.\n\n\n, total1);

    do {
        puts(Do you think the next roll will be );
        puts((H)igher, (L)ower, or (S)ame?\n);
        puts(Enter H, L, or S to reflect your guess.);
        scanf( %c, &ans);
        ans = toupper(ans);
    } while ((ans != 'H') && (ans != 'L') && (ans != 'S'));

    // Roll the dice a second time to get your second total
    dice1 = (rand() % 5) + 1;
    dice2 = (rand() % 5) + 1;
    total2 = dice1 + dice2;

    // Display the second total for the user
    printf(\nThe second roll was %d and %d, , dice1, dice2);
    printf(for a total of %d.\n\n, total2);

    if (ans == 'L'){
        if (total2 < total1){
            printf(%d is lower than %d\n, total2, total1);
        }
        else
        {
            printf(Wrong! %d is not lower than %d\n\n, total2, total1);
        }
    }else if (ans == 'H'){
        if (total2 > total1){
            printf(%d is higher than %d\n, total2, total1);
        }else{
            printf(Sorry! %d is not higher than %d\n\n, total2,total1);
        }
    }else if (ans == 'S'){
        if (total2 == total1){
            printf(%d is the same as %d\n\n, total2, total1);
        }else{
            printf(Sorry! %d is not the same as %d\n\n,total2, total1);
        }
    }

    return(0);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐