gpt4 book ai didi

在 while 语句中将旧变量值与新值进行比较?

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:06 25 4
gpt4 key购买 nike

为什么此代码不多次打印“New time”?是否可以在不在循环外调用 clock() 的情况下进行修复?

#include <stdio.h>
#include <time.h>
int main(char argv[], int argc)
{
double lastTime = 0;
while(1)
{
printf("New time:\n");
while(lastTime == (lastTime = (double)clock() / (double)CLOCKS_PER_SEC))
{
printf("%f\n", lastTime);
}
}
return 0;
}

最佳答案

你的代码好像被截断了。但是你的问题是这个逻辑:

while (lastTime == (lastTime = something))

测试永远为真;您为 lastTime 分配了一些值,然后将其与自身进行比较。

关于在 while 语句中将旧变量值与新值进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9808962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com