C语言输入和随机数使用猜硬币正反面

155 阅读1分钟
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    /* code */
    int input;
    int coin;
    char* result[] = {"","up","down"};

    srand(time(NULL));
    printf("up is 1, down is 2, if input others then the end.\n");
    while(1){
        coin = rand()%2 + 1;
        printf("result, guess it 1 or 2: ");
        scanf("%d", &input);
        if(input < 1 || input > 2){
            printf("please input the number 1 or 2");
            break;
        }else{
            printf("input: %s the coin's : %s\n", result[input], result[coin]);
            printf("%s\n", (input == coin) ? "right" : "wrong");
        }
        printf("\n");

    }
    return 0;
}