调试和构建在CLion上运行的区别
我刚开始编程,我正在为我写的这段代码而挣扎调试和构建在CLion上运行的区别,c,C,我刚开始编程,我正在为我写的这段代码而挣扎 #include <stdio.h> #include <stdlib.h> #include <time.h> #define FACE 6 enum Status {WON, LOST, CONTINUE}; int RollDice (void); int main() { enum Status gameStatus; int sum = RollDice(); int myP
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FACE 6
enum Status {WON, LOST, CONTINUE};
int RollDice (void);
int main() {
enum Status gameStatus;
int sum = RollDice();
int myPoint;
switch (sum){
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
break;
}
while (gameStatus == CONTINUE){
sum = RollDice();
if (sum == myPoint)
gameStatus = WON;
else if (sum == 7)
gameStatus = LOST;
}
printf ("\n%s", (gameStatus==WON)? "WON":"LOST");
return 0;
}
int RollDice (void) {
int die[2];
srand(time(NULL));
for(int j = 0; j < 2; j++)
die[j] = 1 + rand() % FACE;
printf ("\n\nRolled %d and %d", die[0],die[1]);
return die[0]+die[1];
}
或
这本身并没有错,但我不希望总是有这样的结果
另一方面,当我尝试调试它时,一切正常,程序在最终printf之前的行为符合预期,最终printf未执行,因此无法获得退出代码0,它只是将我重定向到某个我不知道的反汇编文件
在XCode(我使用mac)上,我没有任何这样的问题,无论是在运行还是卸载时。
有人能帮我理解吗
非常感谢Call
srand(time(NULL))
在大多数常见系统上仅一次(在最开始时)time
函数以秒为单位返回指定起点后的时间。这意味着如果在同一秒内调用time(NULL)
两次,它将返回相同的值。现在想想如果你用相同的起始值重置随机数生成器两次会发生什么。非常感谢。有人能解释一下,为什么在您建议的更正之前,Xcode运行的程序与CLion不同,CLion被设置为使用Xcode编译器?
Rolled 6 and 4
Rolled 6 and 4
WON
Process finished with exit code 0
Rolled 3 and 3
Rolled 3 and 3
WON
Process finished with exit code 0