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

#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);
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/266766.html

(0)
上一篇 2022年6月7日 23:29
下一篇 2022年6月7日 23:32

相关推荐

发表回复

登录后才能评论