2的倍数条件不工作(反问题)
The multiples of 2 Condition is not working(Counter Problem)
从技术上讲,我的这段代码是一个水壶模拟,但现在的问题是。当它达到20.00时,它的代码不能工作
#include<stdio.h>
#include<stdlib.h>
#include<cstdlib>
#include<time.h>
//Global Declaration for waterTemperature
double nWaterTemp;
//Function for seconds :)
void wait ( int seconds ){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {
}
}
//Function for increasing of hit of Kettle
void behaviourOn(){
int counter = 1;
while(nWaterTemp <=100.0){
wait(1);
printf("Ticking! %dn",counter);
if( counter%2 == 0&& nWaterTemp >=20.0 ){
nWaterTemp+=1.5/2.0;
printf("The water temp is %.2fn",nWaterTemp);
}else if(counter%3==0){
nWaterTemp+=2.0/3.0;
printf("The water temp is %.2fn",nWaterTemp);
}else if(nWaterTemp == 20.0){
system("cls");
printf("THE KETTLE IS NOW ON!n");
}
counter++;
}
}
//Function for Envinronment
void environment(){
int counter2 = 0;
system("cls");
printf("THE WATER IS NOW COOLINGn");
while(nWaterTemp>=0){
counter2++;
wait(1);
printf("Ticking! %dn",counter2);
if(counter2%3==0){
nWaterTemp -=2.0/3.0;
printf("The water temp is %.2fn",nWaterTemp);
}
}
}
//main
int main(void){
behaviourOn();
environment();
system("pause");
}
你看,如果水的温度不高于20.00,它只会每2秒增加一次,但在我的代码中,每1秒它的值就会改变几次,而且每2秒它就会改变几次……这段代码中的错误是什么?
if( counter%2 == 0&& nWaterTemp >=20.0 ){//THe equation changes if it reach 20.00, and the heat increases every 2 seconds
nWaterTemp+=1.5/2.0;
printf("The water temp is %.2fn",nWaterTemp);
这是让我感到困惑的部分,正如你在条件中看到的它应该每2秒增加一次温度值但事实是它每1秒改变一次有时每1秒改变一次请帮忙
你应该这样写:
if( nWaterTemp >=20.0 ) {
if (counter%2 == 0) {
nWaterTemp+=1.5/2.0;
printf("The water temp is %.2fn",nWaterTemp);
}
} else if ...
否则当counter % 2 != 0
时,它会出错
相关文章:
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 餐饮哲学家问题 - 只有 2 个线程工作
- 如何使这个 Foor 循环在这个问题中工作?
- rand()函数在特定问题中的工作
- 我在循环中的循环中遇到问题,无法正常工作
- 这个餐饮哲学家问题(dpp)的解决方案是如何工作的?互斥体和信号量
- 我在while循环中遇到问题,我只是无法弄清楚为什么它无法按预期工作
- POSIX 的问题。线程无法正常工作
- Qt Ros工作区中的QVTKWidget问题
- 而循环不工作终止问题
- 解决死锁问题,在主线程中等待多个工作线程完成 (C++11)
- (c++)无法让 void 函数工作,主.cpp内部的几行也有问题
- nqueens问题的C 递归解决方案无法正常工作
- 虽然这个简单的 C++ 程序可以打印 2 的幂,但它有很多问题时可以工作
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- 如何使其工作,两个类的问题
- 回文字符串问题:为什么我必须放+1而不是-1才能让这个代码工作
- 我的 IF 语句中存在问题,否则无法正常工作
- 在 ROS 工作区中使用 catkin_make 时 Qt 出现问题
- 有问题使循环工作与功能相结合