自己做了一个抽卡小程序
#include
#include
#include
//定义抽卡概率
#define RARITY_SSR 0.08
#define RARITY_SR 0.25
#define RARITY_R 0.67
//定义抽卡的道具
const char *SSR[] = {"SSR1","SSR2","SSR3","SSR4"};
const char *SR[] = {"SR1","SR2","SR3","SR4","SR5","SR6"};
const char *R[] = {"R1","R2","R3","R4","R5","R6","R7","R8"};
//定义一次抽卡的函数
char *drawCard(){
double random = (double)rand()/RAND_MAX; //生成0-1之间的随机数
if(random <= RARITY_SSR){ //SSR稀有度
int index = rand() % 4; //取0-3之间的随机数
return SSR[index];
}
else if(random <= RARITY_SR){ //SR稀有度
int index = rand() % 6; //取0-5之间的随机数
return SR[index];
}
else{ //R稀有度
int index = rand() % 8; //取0-7之间的随机数
return R[index];
}
}
int main(){
srand((unsigned)time(NULL)); //根据系统时间初始化随机种子
printf("十连抽卡结果:\n");
for(int i = 0; i < 10; i++){
printf("第%d次抽卡:%s\n", i+1, drawCard());
}
return 0;
}
结果显示如下报错
20 25 [Error] invalid conversion from 'const char*' to 'char*' [-fpermissive]
加上 const 修饰下,你定义的 SSR,SR,R 都是 const修饰的,函数的返回值也应该 const修饰
编译器检测到了,函数返回值有可能被修改,因此必须定义为 const修饰的
//定义一次抽卡的函数
const char *drawCard()
{
}
你前面SSR、SR、R都定义的是常量const char,但drawCard函数定义的是char,报错的意思就是说你在char函数里使用了const char。你把SSR、SR、R都改成char,或者把drawCard函数改成const char都可以。
运行结果: