为什么面积返回为 0?
这是代码。
int a;
int pi = 3.14;
int area;
int main()
{
cout << "Input the radius of the circle ";
cin >> a;
a *= a *= pi >> area;
cout << "The area is " << area;
}
Here's the code.
int a;
int pi = 3.14;
int area;
int main()
{
cout << "Input the radius of the circle ";
cin >> a;
a *= a *= pi >> area;
cout << "The area is " << area;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
>>
运算符与数字一起使用时是右移,而不是赋值。您想要类似更新的
内容,您还需要使用浮点类型,否则您的答案将不是您所期望的。
The
>>
operator when used with numbers is right shift, not assignment. You want something likeUpdate
You also need to use a floating point type or your answer won't be what you expect.
我没有足够的耐心破译你奇怪的密码。仅仅
area = a * a * pi
怎么样?I don't have enough patience to decipher your strange code. How about just
area = a * a * pi
?你的代码没有任何意义。
pi
(以及所有其他变量)需要是 double 或 float,...而不是 int。 int 只能包含整数。而pi
显然不是积分。a *= a *= pi >>> area;
应该是area = a * a * pi;
>>
是位移,而不是对右侧的赋值*=
是乘法赋值而不仅仅是乘法。即它类似于left=left*right
Your code doesn't make any sense.
pi
(and all your other variables) need to be double or float,... not int. An int can only contain an integral number. Andpi
is obviously not integral.a *= a *= pi >> area;
should bearea = a * a * pi;
>>
is a bitshift, not an assignment to the right side*=
is multiply assign and not just multiply. i.e. it is similar toleft=left*right
圆的面积是 pi * r * r 因此你会想要这样做;
a = a * a * pi
希望有所帮助
,它们都需要是浮点数。
The area of a circle is pi * r * r therefore you would want to do;
a = a * a * pi
Hope that helps
and they all would need to be floats.
你的代码没有做我认为你想要它做的事情。您不可以使用
>>
给变量赋值;这仅用于流提取(和位移)。另外,
a *= a *= pi
可能不会按照您的想法进行操作。另外,您需要浮点值,而不是
int
。 “int”pi 只是 3。此外,您应该对流提取进行错误检查!
尝试:
Your code doesn't do what I think you wanted it to do. You don't assign to variables with
>>
; that is only for stream extraction (and bitshifting).Also,
a *= a *= pi
probably doesn't do what you think it does.Also, you want floating-point values, not
int
. An "int" pi is just 3.Also, you should have error checking on your stream extraction!
Try:
数据类型错误。将双精度值分配给
int
?那是错误的。写下:
同样,将其他数据类型也更改为 double 。
Wrong datatype. Assigning double value to
int
? That's wrong.Write this:
And likewise, change other datatypes to
double
as well.因为您对所有变量都使用
int
或整数。您想要使用double
甚至float
。 (double
更精确)。Because you're using
int
, or integer, for all your variables. You want to usedouble
s or evenfloat
s. (double
s are more precise).所有变量都声明为 int,这只是删除分配给它的任何小数部分。要使用浮点值,请使用 double。
另外,你的方程几乎难以理解。不确定你想在那里做什么。
All your variables are declared as int, which simply drops any fractional portion assigned to it. To work with floating-point values, use double instead.
Also, your equation in almost incomprehensible. Not sure what you're trying to do there.