三角形的面积在输出中显示为零,这是为什么?
我做错了什么??
#include <iostream>
using namespace std;
int main() {
int Base, Height, Area;
// >>> Is anything wrong with the formula??
Area = (0.5) * Height * Base;
cout << "To find the Area of Triangle" << endl << endl;
// Base
cout << "Enter Base length:";
cin >> Base;
cout << endl;
// Height
cout << "Enter Height length";
cin >> Height;
cout << endl << endl;
cout << "Your Base Length is:" << Base << endl;
cout << "Your Height Length is:" << Height << endl;
// calculating area of triangle
// >>> This is the part output is zero
cout << "Area of the triangle is :" << Area << endl;
}
Run Code Online (Sandbox Code Playgroud)
当您有一个计算值时,您必须对所涉及的值进行任何更改来计算它。与代数不同,其中的x = y * 2意思是“x根据定义y乘以 2”,在 C++ 中,它的意思是“分配给现在x的y时间值”并且与未来无关。2
对于计算出来的东西,你使用一个函数:
int Area(const int Height, const int Base) {
return 0.5 * Height * Base;
}
Run Code Online (Sandbox Code Playgroud)
现在你可以在哪里调用它:
cout<<"Area of the triangle according to your measurements is :"<<Area(Height, Base)<<endl<<endl;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |