有一个非常简单的代码,我用C++构建.这是我的第一个C++代码,所以我不完全确定某些地方的语法.但是,对于以下代码,我的for循环根本没有运行!我不明白为什么不...有人能发现问题吗?
#include <cstdlib>
#include <cmath>
using namespace std;
int main () {
/*
* Use values for wavelength (L) and wave number (k) calculated from linear
* dispersion program
*
*/
//Values to calculate
double u; //Wave velocity: d*phi/dx
double du; //Acceleration of wave: du/dt
int t;
//Temporary values for kn and L (taken from linear dispersion solution)
float L = 88.7927;
float kn = 0.0707624;
Run Code Online (Sandbox Code Playgroud)
注意:我省略了变量声明以节省空间.
/*
* Velocity potential = phi = ((Area * g)/omega) * ((cosh(kn * (h + z)))/sinh(kn * h))*cos(k*x - omega * t);
* Velocity of wave, u = d(phi)/dx;
* Acceleration of wave, du = du/dt;
*/
for (t = 0; t == 5; t++) {
cout << "in this loop" << endl;
u = ((kn * A * g)/omega) * ((cosh(kn * (h + z)))/sinh(kn * h)) * cos(omega * t);
du = (A * g * kn) * ((cosh(kn * (h + z)))/sinh(kn * h)) * sin(omega * t);
cout << "u = " << u << "\tdu = " << du << endl;
}
cout << L << kn << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我把"在这个循环中"作为测试,它不会进入循环(编译好)..
提前谢谢你看看这个!
t初始化为0,t == 5将始终被评估为false,因此您的for循环将永远不会运行.
更新
for (t = 0; t == 5; t++) {
Run Code Online (Sandbox Code Playgroud)
至
for (t = 0; t < 5; t++) {
Run Code Online (Sandbox Code Playgroud)
声明
重复执行一个语句,直到条件变为false.
for(init-expression; cond-expression ; loop-expression)
Run Code Online (Sandbox Code Playgroud)statement;