这可能是我的代码,但我可以通过命令行编译和运行这个程序。在 Windows 中,exe 文件也是可运行的。但是,我不能在任何 unix 系统(ubuntu 或 osx)上在终端之外运行编译的代码我对 C 很陌生,所以任何帮助将不胜感激。谢谢!
澄清:
在 Ubuntu 中我运行
gcc game.c -o game
./game
然后它运行完美。但是,如果我通过 GUI 进入游戏文件并双击它,它不会做任何事情。我习惯在 Windows 上它会调出一个命令并运行程序,就好像你从cmd
.
代码(这是一个简单的猜数字游戏):
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int guessed = 0;
int guesses = 0;
int total_guesses = 0;
int games_played = 0;
int range_top = 10;
int generate_random_number()
{
srand(time(NULL));
return (rand() % range_top);
}
void take_guess(int num)
{
int guess;
printf("what is your guess: ");
scanf(" %i",&guess);
if(guess == num)
{
guessed = 1;
}
else if(guess>num)
{
printf("Your guess was too high,\n");
}
else
{
printf("Your guess was too low.\n");
}
}
void print_stats()
{
printf("\n\n\nGames Played: %i\nTotal Guesses: %i\nAverage Guesses Per Game: %f\n\n\n\n",games_played,total_guesses,(double)total_guesses/games_played);
int i = 5;
while(i>0)
{
printf("exiting in %is\n",i);
i--;
sleep(1);
}
}
int main(void)
{
printf("This is a game in C\n");
printf("Would you like to play? (y/n): ");
char play;
scanf("%c",&play);
while(play == 'y')
{
printf("I am thinking of a number between 0 and %i\n",range_top);
int num = generate_random_number();
while(guessed ==0)
{
take_guess(num);
guesses++;
}
games_played++;
total_guesses+=guesses;
printf("It took you %i guesses to win.\n",guesses);
printf("Would you like to play again? (y/n): ");
scanf(" %c",&play);
guessed = 0;
guesses = 0;
}
print_stats();
}