我正在编写一个程序,其中包含一个while循环,读取两个double数,并将它们打印出来。该程序还会输出哪个数更大,哪个数更小。
以下是我目前的代码:
int main()
{
// VARIABLE DECLARATIONS
double a;
double b;
while (a,b != '|') //WHILE A & B DO NOT EQUAL '|'
{
cin >>a >>b;
cout << a << b << "\n" ;
if (a<b) //IF A<B: SMALLER VALUE IS A
cout << "The smaller value is:" << a << endl
<< "The larger value is:" << b << endl ;
else if (b<a) //ELSE IF B<A
cout << "The smaller value is:" << b << endl
<< "The larger value is:" << a << endl ;
else if (b==a)
cout << "The two numbers you entered are equal." << "\n" ;
}
}
下一步是,如果这两个数字相差小于1.0/10000000,则让程序输出“the numbers are almost equal”。我该如何做到这一点?
while (a,b != '|')
是什么意思吗? - Kiril Kirovwhile (a != '|' && b != '|')
。而你现在使用了逗号运算符,它会先计算a
,然后计算b != '|'
,最终结果是b != '|'
。换句话说,你只有while (b != '|')
,忽略了a
。至于为什么要这样比较,我还不清楚。 - GManNickG