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

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

编程学习网为您整理以下代码实例,主要实现:掷两个骰子并呈现总数,然后要求用户猜测下一个总数,希望可以帮到各位朋友。

#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);
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐