比较输入的字符串和打印的函数

时间:2018-10-31 11:59:09

标签: c visual-studio

下面是我为骰子游戏cho han写的代码。为了输入您的猜测,我使用数字来表示单词“奇数”和“偶数”。从那时起,我尝试再次编写它,但是实际上在scanf部分中编写了奇数甚至偶数,但是无法使其正常工作。任何帮助将不胜感激:)

//cho-han

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

int main(void)
{
srand(time(NULL));

int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;

printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter '1'. To guess even, enter '2'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");

scanf_s("%d", &guess);
if (guess == 2)
{
    printf("\nyour guess is even.\n");
}
if (guess == 1)
{
    printf("\nyour guess is odd.\n");
}
if (guess > 2 || guess < 1)
{
    printf("\nInvalid guess.\nYou lose!\n");
    return (1);
}

printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);

result = x + y;
printf("\ncombined total of both rolls is %d", result);

if (result == 1 || result == 3 || result == 5 || result == 7 || result == 9 || result == 11)
{
    printf("\ncombined total of both rolls is odd.\n");
}
else
{
    printf("\ncombined total of both rolls is even.\n");

}

if (guess == 1 && result == 1 || guess == 1 && result == 3 || guess == 1 && result == 5 || guess == 1 && result == 7 || guess == 1 && result == 9 || guess == 1 && result == 11)
{
    printf("\nYou win!\n");
}
else if (guess == 2 && result == 2 || guess == 2 && result == 4 || guess == 2 && result == 6 || guess == 2 && result == 8 || guess == 2 && result == 10 || guess == 2 && result == 12)
{ 
    printf("\nYou win!\n");
}
else
{
    printf("\nYou lose!\n");
}
return 0;

}

2 个答案:

答案 0 :(得分:0)

  1. 您应将scanf_s更改为scanf
  2. if (result == 1 || result == 3 ...行可能是if (result % 2 == 1) {
  3. 您可以使用strcmp来解决您的问题

以下code可以工作:

   //cho-han

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

int main(void)
{
    srand(time(NULL));

    int x = (rand() % 6) + 1;
    int y = (rand() % 6) + 1;
    int result = 0;
    int guess = 0;
    char buf[10];

    printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
    printf("To guess odd, enter 'odd'. To guess even, enter 'even'.\n\n");
    printf("Please enter your guess for the combined total of the two dice rolls: ");

    fgets(buf, sizeof buf, stdin);
    if (strcmp(buf, "even\n") == 0) {
        guess = 2;
        printf("\nyour guess is even.\n");
    } else if (strcmp(buf, "odd\n") == 0) {
        guess = 1;
        printf("\nyour guess is odd.\n");
    } else {
        printf("\nInvalid guess.\nYou lose!\n");
        return 1;
    }

    printf("\ndice roll 1 = %d\n", x);
    printf("dice roll 2 = %d\n", y);
    printf("\ncombined total of both rolls is %d", x + y);

    result = (x + y) % 2;
    if (result == 1)
        printf("\ncombined total of both rolls is odd.\n");
    else
        printf("\ncombined total of both rolls is even.\n");

    if (guess == result)
        printf("\nYou win!\n");
    else
        printf("\nYou lose!\n");

    return 0;
}

答案 1 :(得分:0)

您需要将猜测更改为char类型,然后将scanf更改为捕获字符串。

char guess[256];
scanf("%s", guess);

然后最好的方法是调用toupper()并使用strcmp()与您的文本进行比较。