首页 > 解决方案 > Xcode不显示输出

问题描述

我被要求用 C 语言编写一个骰子游戏,其中计算机和用户充当游戏的竞争双方。首先由计算机生成一个随机数,然后接受用户输入的字符串“g”命令生成用户的随机数(模拟掷一次骰子),并比较它们的值。如果用户得到的随机数小于计算机得到的随机数,则输出“对不起,你输了!”,否则,输出“恭喜你,你赢了!”。

编码:

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
 srand((unsigned)time(NULL));
 int com_inp = (rand() % (6 - 1 + 1)) + 1;
 int user_inp=0;
 char ch;
 scanf("%c",&ch);
 if(ch=='g')
 user_inp = (rand() % (6 - 1 + 1)) + 1;

  if(user_inp<com_inp)
     printf("\n Sorry, you lost!");
  else
     printf("\n Congratulations, you won!");

     printf("\n Computer dice = %d \t User dice = %d",com_inp,user_inp);

  return 0;
}

在 Xcode v12.2 上运行后,没有显示输出。控制台是空白的。

但是,如果程序在 Dev-C++(Windows) 上运行,则会显示输出。

任何帮助,将不胜感激。

标签: c

解决方案


我没有看到之前打印的任何内容scanf。我不熟悉 Windows 上的 Dev-C++,但在 Xcode 控制台中没有提示显示它正在等待输入,因此您可能想要输入如下内容:

printf("Enter a number between 1 and 6\n>");

在你的scanf.

您可能还会遇到一些行缓冲问题。尝试\n在您打印的每一行的末尾添加。

  if(user_inp<com_inp)
     printf("\n Sorry, you lost!\n");
  else
     printf("\n Congratulations, you won!\n");

     printf("\n Computer dice = %d \t User dice = %d\n",com_inp,user_inp);

推荐阅读