我正在为一个简单的基于文本的游戏编写代码,当我购买汽车时,HP 和 TOPSPEED 注册的整数与指定的不同。 我自己查看了代码,也许我没有看到它,但是 Dodge Intrepid 应该以 214hp 和 140mph 的最高速度注册,但是当我进入比赛菜单时,它注册为 320hp 和 160mph 的最高速度,这是Mitsubishi 3000GT 和最后一个 Beta 的 Dodge Stealth 的设置。我从之前的测试版中导入了“比赛”代码,小心地省略了关于之前测试版中使用的汽车的任何信息。如果您能发现我的错误或指出任何问题,将不胜感激(我在帖子中包含了代码)。谢谢你的时间。 (我可能忽略了一些东西)(用 C 编写,用 DevC++ 编译和链接)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*HP at RACE is malfunctioning, look over code.*/
int rnd(int range);
void seedrnd(void);
int main()
{
char name[15];
char ocar;
char exit;
char partsA;
char partsB;
char partsC;
char choice;
char dealer;
char model;
int bhp;
int ohp;
int tspd;
int otspd;
int score;
int oscore;
int cash;
int bonus;
int parts1;
int parts2;
int parts3;
int car1;
int car2;
int car3;
int car;
int hp1;
int hp2;
int hp3;
int tspd1;
int tspd2;
int tspd3;
printf("What is your name?\n");
scanf("%s",name);
parts1=1;
parts2=1;
parts3=1;
hp1=214;
tspd1=140;
hp2=490;
tspd2=190;
hp3=320;
tspd3=160;
car=0;
car1=0;
car2=0;
car3=0;
cash=40000;
while(exit!='-')
{
printf("\nMenu:\n1.Garage\n2.Race\n3.Parts Shop\n4.Dealerships\n\n");
choice=getch();
if(choice=='1')
{
printf("Welcome to %s's garage:\n\n",name);
if(car1>0)
{
printf("1.Dodge Intrepid\n");
}
if(car2>0)
{
printf("2.1988 Vector M12\n");
}
if(car3>0)
{
printf("3.Mitsubishi 3000GT\n");
}
car=getch();
if(car=='1')
{
if(car1>0)
{
printf("You are now driving a Dodge Intrepid\n");
car=1;
}
if(car1<1)
{
printf("You do not yet own this vehicle\n");
}
}
if(car=='2')
{
if(car2>0)
{
printf("You are now driving a 1988 Vector M12\n");
car=2;
}
if(car2<1)
{
printf("You do not yet own this vehicle\n");
}
}
if(car=='3')
{
if(car3>0)
{
printf("You are now driving a Mitsubishi 3000GT\n");
car=3;
}
if(car<1)
{
printf("You do not yet own this vehicle\n");
}
}
}
if(choice=='2')
{
if(car=1)
{
bhp=hp1;
tspd=tspd1;
}
if(car=2)
{
bhp=hp2;
tspd=tspd2;
}
if(car=3)
{
bhp=hp3;
tspd=tspd3;
}
/*Begin Race Mechanism*/
printf("Your selected car has %dhp and %dmph top speed\n\n",bhp,tspd);
printf("Now choose your opponent:\n");
printf("1.Rachel (150-250hp 110-130mph)\n");
printf("2.Kyle (250-350hp 130-170mph)\n");
printf("3.Darrian (350-450hp 170-210mph)\n");
printf("4.Chelsea (450-550hp 210-220mph)\n");
ocar=getch();
seedrnd();
if(ocar=='1')
{
ohp=rnd(100)+151;
otspd=rnd(20)+111;
bonus=500;
}
else if(ocar=='2')
{
ohp=rnd(100)+251;
otspd=rnd(40)+131;
bonus=1000;
}
else if(ocar=='3')
{
ohp=rnd(100)+351;
otspd=rnd(40)+171;
bonus=1500;
}
else if(ocar=='4')
{
ohp=rnd(100)+451;
otspd=rnd(10)+211;
bonus=2000;
}
else
{
printf("Haha, you're racing Eli\nNo contest here\n");
ohp=2;
otspd=25;
}
printf("Match-up:%s %dhp %dmph top speed\n",name,bhp,tspd);
printf(" vs \n");
printf("Opponent: %dhp %dmph top speed\n",ohp,otspd);
getch();
score=bhp*tspd;
oscore=ohp*otspd;
printf("Let the race begin\n");
sleep(1000);
printf("3\n");
sleep(1000);
printf("2\n");
sleep(1000);
printf("1\n");
sleep(1000);
printf("!\n\n");
sleep(2000);
printf("The race is over, and the winner is!!!\n\n\n\n");
sleep(3000);
if(score>oscore)
{
printf("YOU!!!\n");
cash=cash+(bonus);
}
else if(score<oscore)
{
printf("Your Opponent...\n");
}
else
{
printf("...neither of you, it was a tie!\n");
cash=cash+(bonus/2);
}
printf("You now have$%d.\n\n",cash);
}/* Closes choice 2*/
if(choice=='3')
{
/* Parts Shop*/
printf("Under Construction\n");
}
if(choice=='4')
{
printf("Press 'y' to exit\n");
printf("Please Select a Dealership\n");
printf("1.Dodge\n2.Vector\n3.Mitsubishi\n\n");
while(dealer!='y')
{
dealer=getch();
if(dealer=='1')
{
printf("DODGE:\n");
if(car1<1)
printf("1.Dodge Intrepid (214hp 140mph) $21,000\n\n");
model=getch();
if(model=='1')
{
if(cash<21000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=21000)
{
if(car1>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car1<1)
{
car1=car1+1;
cash=cash-21000;
printf("Thank You for purchasing this Dodge Intrepid\n\n");
}
}
}
}
if(dealer=='2')
{
printf("VECTOR:\n");
if(car2<1)
printf("1.1988 Vector M12 (490hp 190mph)$180,000\n\n");
model=getch();
if(model=='1')
{
if(cash<180000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=180000)
{
if(car2>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car2<1)
{
car2=car2+1;
cash=cash-180000;
printf("Thank You for purchasing this 1988 Vector M12\n\n");
}
}
}
}
if(dealer=='3')
{
printf("MITSUBISHI:\n");
if(car3<1)
printf("1.Mitsubishi 3000GT (320hp 160mph) $60,000\n\n");
model=getch();
if(model=='1')
{
if(cash<60000)
{
printf("You cannot afford this vehicle\n\n");
}
if(cash>=60000)
{
if(car3>0)
{
printf("You have already purchased this vehicle\n\n");
}
if(car3<1)
{
car3=car3+1;
cash=cash-60000;
printf("Thank You for purchasing this Mitsubishi 3000GT\n\n");
}
}
}
}
}
}
exit=getch();
}
return(0);
}
int rnd(int range)
{
int i;
i=rand()%range;
return(i);
}
void seedrnd(void)
{
srand((unsigned)time(NULL));
}
最佳答案
if(car=1)
{
bhp=hp1;
tspd=tspd1;
}
你的意思可能是 if( car == 1 ) {
...这只是将 car
设置为 1,然后“返回”1
到 if
。然后对 car == 2
和 car == 3
执行相同的操作。
=
是赋值运算符,而 ==
是比较运算符!第一个为变量赋值,而后者在相同时返回 true(注意,对于 char*
,您需要 strcmp()
)。
通常您应该将您的程序分解成小函数并检查不满意的情况(例如,如果用户输入 Not Acceptable 内容会发生什么?)
还有一点你要注意的是这个
scanf("%s",name);
如果有人输入超过 15 个字符怎么办?您将开始在您不拥有的内存中进行调整,这会调用 Undefined Behaviour .一个简单的技巧是做
scanf("%14s", name);
这会将输入限制为 14 个字符(并将保留第 15 个字符作为 nul 终止符)。在这里阅读更多:http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
正如其他人在评论中指出的那样,考虑使用结构让您的生活更轻松。我没有太详细地阅读该程序以了解您游戏的确切结构,但是您可以为用户提供例如一个结构:
struct player
{
char name[15];
car* owned_cars; //Linked List!
}
car
的定义如下:
struct car
{
int max_speed;
int colour; //or even better an enum here
int seats;
//... etc.
car* next_car; //Make the Linked List's next node here!
}
还有!如果您创建一个 LinkedList,通常会发生的一个错误是它们忘记将它们的指针初始化为 NULL
。所以:
car* new_car = malloc( sizeof(car) );
new_car->next_car = NULL;
如果您不这样做,next_car
将包含一个随机/垃圾号码!因此,当您尝试遍历列表时
while(new_car->next_car != NULL)
{
//....
}
您将访问不属于您的内存。
祝你好运,玩得开心!
关于c - C编程,整数分布错误,找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8016095/