c - 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) 上运行,则会显示输出。
任何帮助,将不胜感激。
解决方案
我没有看到之前打印的任何内容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);
推荐阅读
- r - 使用 install.packages 安装软件包时 RStudio 崩溃
- r - 创建一个弹出窗口,显示一旦访问闪亮的应用程序,欢迎用户访问仪表板
- haskell - 如何将数字作为字符串添加到haskell中的字符串
- python - 如何使用 python 执行可恢复的文件上传到 Google Drive
- vb.net - 如果 notEOF 和 bof 已经放了,仍然报错
- c - MPI 进程中 fprintf 的原子性
- python - 检查熊猫 df 中列的值是否在列表中
- react-native - Redux 获取返回空的有效负载数组
- java - 实时缓存 401 中找不到 PayPal 访问令牌
- maven - 运行自由式 maven-gatling 项目时 Jenkins 工作失败