将输入的字符串与打印的函数进行比较

标签 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;

}

最佳答案

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

以下代码可以工作:

   //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;
}

关于将输入的字符串与打印的函数进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082849/

相关文章:

我可以在 C 程序中使用 GET 方法吗

c# - 以编程方式为 .sln 添加构建配置

c# - 允许在 visual studio 编辑器扩展中插入断点

c++ - 简单类 C++ 的 2019 错误

c++ - Win32 项目生成 MFC 错误

c - 如何在linux内核模块中添加RTC定时器

对 malloc 的调用永远不会在嵌入式系统上返回——奇怪的是

c++ - 我们如何远程调试Linux服务器上运行的C++应用程序?

c - LWIP Makefile 错误

visual-studio - VS 2010 是否有计划的 StyleCop 版本?