新人做题目时遇到的问题,可编译但无法运行!特来求助!
本帖最后由 时生 于 2016-10-2 22:28 编辑这个程序就是一个猜数游戏,电脑随机在1-20之间选取一个数字,然后用户猜测这个数字是多少,猜错4次则失败。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, answer, range, Time = 0; /*number是随机数,answer是用户输入的数字,range为了定义随机数的范围,Time是为了计算用户输入的次数*/
range = 20 - 1; /*产生一个1-20的随机数*/
srand(time(NULL));
number = rand() % range + 1;
printf("I am thinking of a number between 1 and 20.\nCan you guess what it is?");
scanf("%d", &answer);
if (answer == number) /*此处先判断答案是否与随机数吻合*/
{
printf("Congratulations!You did it!\n");
}
else
{
while ((answer != number) || (Time < 4)) /*此处开始循环体,直至answer与number相等或者用户输入次数大于4次*/
{
if (answer > number)
{
printf("Your guess is low. Try again:");
}
else
{
printf("Your guess is high, Try again:");
}
Time = Time + 1;
scanf("%d", answer);
}
/*此处开始用来判断是什么条件导致循环结束,然后分别输出对应结果*/
if(answer == number)
{
printf("Congratulations!You did it!\n");
}
else
{
printf("Sorry. The number was %d.\nYou should have gotten it by now.\nBetter luck next time.", number);
}
}
return 0;
}
运行到第一次输入后错误,提示重新尝试后就不行了!显示的错误好像是什么Signal SIGSEGV =。=看不懂。。。。
求各位大神帮忙看看啊{:5_92:}
另外,我用的软件是Dev-c++,调试的时候总是出现问题,然后就Dev-c++无法运行,提示关闭了OTZ想问下是什么问题。。。 本帖最后由 mingcxx 于 2016-10-3 01:34 编辑
有四处错误,做了注释:#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, answer, range, Time = 0; /*number是随机数,answer是用户输入的数字,range为了定义随机数的范围,Time是为了计算用户输入的次数*/
range = 20 - 1;
srand(time(NULL));
number = rand() % range + 1;
printf("I am thinking of a number between 1 and 20.\nCan you guess what it is?");
scanf("%d", &answer);
Time = Time + 1; //1. 第一次
if (answer == number)
{
printf("Congratulations!You did it!\n");
}
else
{
while ((answer != number) && Time < 4)
/*2. while ((answer != number) || (Time < 4))*/
{
if (answer < number)
/*3. if (answer > number)*/
{
printf("Your guess is low. Try again:");
}
else
{
printf("Your guess is high, Try again:");
}
Time = Time + 1;
scanf("%d", &answer);
/*4. scanf("%d", answer);*/
}
if (answer == number)
{
printf("Congratulations!You did it!\n");
}
else
{
printf("Sorry. The number was %d.\nYou should have gotten it by now.\nBetter luck next time.", number);
}
}
return 0;
} mingcxx 发表于 2016-10-3 01:33
有四处错误,做了注释:
!!!!我真是眼瞎检查了好多遍都没看到那个寻址符的错误!!!OTZ!!!还有或和与的问题我也纠结了一下,最后选择了错误的那个XDD总之非常感谢!谢谢啦!
页:
[1]