为什么这个 while 循环不显示第一个元素?
Why doesn't this while loop show the first element?
我是 C++ 的初学者,想做 this 练习。以下是我的代码,但我不明白为什么它在while循环中不显示下限。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double low=-0.000001, high=-0.000001, step=0.0, stop, f;
while (low <= -0.000001 || low > 50000) {
cout << "Lower limit (0~50,000): "; cin >> low;
if (low <= -0.000001) {cout << "Must be positive." << endl;}
else if (low > 50000) {cout << "Out of range." << endl;}
}
while (high <= -0.000001 || high > 50000 || high <= low) {
cout << "Higher limit (0~50,000): "; cin >> high;
if (high <= -0.000001) {cout << "Must be positive." << endl;}
else if (high > 50000) {cout << "Out of range." << endl;}
else if (high <= low) {cout << "Must higher than lower limit." << endl;}
}
while (step <= 0 || step > high) {
cout << "Step (0.000001~" << high << "): "; cin >> step;
if (step <= 0) {cout << "Must be positive." << endl;}
else if (step > high) {cout << "Out of range." << endl;}
}
cout << endl << "Celsius\t\tFahrenheit" << endl;
cout << "-------\t\t----------" << endl;
stop = low;
while (stop < high) {
f = 1.8 * stop + 32.0;
stop += step;
cout << fixed << setprecision(6) << stop << "\t" << fixed << setprecision(6) << f << endl;
}
return 0;
}
例如,如果我输入 1.5 作为下限,我想它的输出应该以 1.5 开头,但它却以 2.0 开头...我该如何解决这个问题?谢谢
Img of Not the outcome I want
第一次 stop
被打印是 在 第一次增量之后:
while (stop < high) {
f = 1.8 * stop + 32.0;
stop += step; <- increment
cout << ... << stop << ... << f << endl; <- print
}
将打印移到上面,它应该可以正常工作。
旁注:使用调试器和重复 "step over" 命令很容易找到此类问题(也许有一些手表显示您感兴趣的值如何随时间变化)。下面一步一步的执行程序,可以有所启发。
我是 C++ 的初学者,想做 this 练习。以下是我的代码,但我不明白为什么它在while循环中不显示下限。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double low=-0.000001, high=-0.000001, step=0.0, stop, f;
while (low <= -0.000001 || low > 50000) {
cout << "Lower limit (0~50,000): "; cin >> low;
if (low <= -0.000001) {cout << "Must be positive." << endl;}
else if (low > 50000) {cout << "Out of range." << endl;}
}
while (high <= -0.000001 || high > 50000 || high <= low) {
cout << "Higher limit (0~50,000): "; cin >> high;
if (high <= -0.000001) {cout << "Must be positive." << endl;}
else if (high > 50000) {cout << "Out of range." << endl;}
else if (high <= low) {cout << "Must higher than lower limit." << endl;}
}
while (step <= 0 || step > high) {
cout << "Step (0.000001~" << high << "): "; cin >> step;
if (step <= 0) {cout << "Must be positive." << endl;}
else if (step > high) {cout << "Out of range." << endl;}
}
cout << endl << "Celsius\t\tFahrenheit" << endl;
cout << "-------\t\t----------" << endl;
stop = low;
while (stop < high) {
f = 1.8 * stop + 32.0;
stop += step;
cout << fixed << setprecision(6) << stop << "\t" << fixed << setprecision(6) << f << endl;
}
return 0;
}
例如,如果我输入 1.5 作为下限,我想它的输出应该以 1.5 开头,但它却以 2.0 开头...我该如何解决这个问题?谢谢
Img of Not the outcome I want
第一次 stop
被打印是 在 第一次增量之后:
while (stop < high) {
f = 1.8 * stop + 32.0;
stop += step; <- increment
cout << ... << stop << ... << f << endl; <- print
}
将打印移到上面,它应该可以正常工作。
旁注:使用调试器和重复 "step over" 命令很容易找到此类问题(也许有一些手表显示您感兴趣的值如何随时间变化)。下面一步一步的执行程序,可以有所启发。