为什么面积返回为 0?

发布于 2024-10-11 21:12:31 字数 242 浏览 10 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

回眸一遍 2024-10-18 21:12:31

>> 运算符与数字一起使用时是右移,而不是赋值。您想要类似

area = a * a * pi;

更新

内容,您还需要使用浮点类型,否则您的答案将不是您所期望的。

float a;
float pi = 3.14f;
float area;

The >> operator when used with numbers is right shift, not assignment. You want something like

area = a * a * pi;

Update

You also need to use a floating point type or your answer won't be what you expect.

float a;
float pi = 3.14f;
float area;
べ映画 2024-10-18 21:12:31

我没有足够的耐心破译你奇怪的密码。仅仅area = a * a * pi怎么样?

I don't have enough patience to decipher your strange code. How about just area = a * a * pi?

童话 2024-10-18 21:12:31

你的代码没有任何意义。

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. And pi is obviously not integral.

a *= a *= pi >> area; should be area = 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 to left=left*right

自由如风 2024-10-18 21:12:31

圆的面积是 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.

对岸观火 2024-10-18 21:12:31

你的代码没有做我认为你想要它做的事情。您不可以使用 >> 给变量赋值;这仅用于流提取(和位移)。

另外, a *= a *= pi 可能不会按照您的想法进行操作。

另外,您需要浮点值,而不是 int。 “int”pi 只是 3。

此外,您应该对流提取进行错误检查!

尝试:

int main()
{
    const float pi = 3.14;
    float a;

    cout << "Input the radius of the circle ";
    if (!(cin >> a)) {
         cout << "Invalid radius!";
         return 1;
    }

    float area = (a * a * pi);

    cout << "The area is " << area;
}

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 main()
{
    const float pi = 3.14;
    float a;

    cout << "Input the radius of the circle ";
    if (!(cin >> a)) {
         cout << "Invalid radius!";
         return 1;
    }

    float area = (a * a * pi);

    cout << "The area is " << area;
}
变身佩奇 2024-10-18 21:12:31
int pi = 3.14;

数据类型错误。将双精度值分配给int?那是错误的。

写下:

double pi = 3.14;

同样,将其他数据类型也更改为 double 。

int pi = 3.14;

Wrong datatype. Assigning double value to int? That's wrong.

Write this:

double pi = 3.14;

And likewise, change other datatypes to double as well.

谢绝鈎搭 2024-10-18 21:12:31

因为您对所有变量都使用 int 或整数。您想要使用double甚至float。 (double 更精确)。

Because you're using int, or integer, for all your variables. You want to use doubles or even floats. (doubles are more precise).

浮生未歇 2024-10-18 21:12:31

所有变量都声明为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文