簡體   English   中英

Xcode 不顯示輸出

[英]Xcode not showing output

我被指示用 C 語言編寫一個骰子游戲,其中計算機和用戶充當游戲的競爭方。 首先由計算機生成一個隨機數,然后接受用戶輸入的字符串“g”命令,生成用戶的隨機數(模擬一次擲骰子),並比較它們的值。 如果用戶得到的隨機數小於計算機得到的隨機數,輸出“Sorry, you loss!”,否則輸出“Congratulations, you win!”。

編碼:

#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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM