C++ 我很难为if/else语句编写此任务的代码。这是第一节编程课
编写一个计算两个三角形面积的程序。向用户询问每个三角形的底部和高度。打印两个三角形的面积。程序应该告诉用户哪个三角形的面积较大,或者面积是否相同。错误检查:不允许用户输入底数或高度的负数,因此请验证输入 这是我到目前为止所做的,但我的程序没有编译C++ 我很难为if/else语句编写此任务的代码。这是第一节编程课,c++,if-statement,C++,If Statement,编写一个计算两个三角形面积的程序。向用户询问每个三角形的底部和高度。打印两个三角形的面积。程序应该告诉用户哪个三角形的面积较大,或者面积是否相同。错误检查:不允许用户输入底数或高度的负数,因此请验证输入 这是我到目前为止所做的,但我的程序没有编译 #include <iostream> using namespace std; int main() { double baseOne, baseTwo, heightOne, heightTwo, areaOne, are
#include <iostream>
using namespace std;
int main()
{
double baseOne, baseTwo, heightOne, heightTwo, areaOne, areaTwo, i1;
cout << "Please enter the base and height of first triangle\n";
cin >> baseOne, heightOne;
if (baseOne <= 0 && heightOne <= 0)
{
cout << "Your value is invalid!!\n";
}
else
{
cout << "What is the length of first triangle?\n";
cin >> baseOne, heightOne;
areaOne = (baseOne * heightOne) / 2;
cout << "Area of the first triangle is" << areaOne << endl;
}
cout << "What is the base and height of second triangle\n";
cin >> baseTwo, heightTwo;
if (baseTwo <= 0 && heightTwo <= 0)
{
cout << "Your value is invalid!!\n";
}
else
{
cout << "What is the length of first triangle?\n";
cin >> baseTwo, heightTwo;
areaTwo = (baseTwo * heightTwo) / 2;
cout << "Area of the first triangle is" << areaTwo << endl;
}
if (areaOne > areaTwo)
{
cout << "Area One is larger with " << areaOne << "than area two\n";
}
else if (areaOne < areaTwo)
{
cout << "Area Two is larger with" << areaTwo << "than area one\n";
}
else
areaOne == areaTwo;
areaOne = i1;
cout << "Magnitude of area one and area two is same with " << i1 <<< endl;
system("pause");
return 0;
}
#包括
使用名称空间std;
int main()
{
双基一、基二、高度一、高度二、区域一、区域二、i1;
库特>基一号,高一号;
如果(baseOne如果您阅读了编译器给您的警告/错误,那么您可以很容易地找出问题所在。我发现您的代码存在以下问题:
areaOne==areaTwo;
我想你想做的是areaOne=
区域二;
前者是检查区域一是否等于
areaTwo
而后者将areaTwo
的值分配给
areaOne
在cout heightOne;
而不是cin>>baseOne,heightOne;
的打字错误与cin>>baseTwo>>HeightTwo类似
double i1
在第7行声明,但从未初始化,您可以直接将其作为第56行的areaOne=i1;
使用。这是错误的,在考虑使用它之前先初始化它
cout>b
您需要使用或代替和,如果,如果其中一个值为负值,该怎么办
正确实施
#include <iostream>
using namespace std;
int main()
{
double baseOne, baseTwo, heightOne, heightTwo, areaOne, areaTwo, i1;
cout << "Please enter the base and height of first triangle\n";
cin >> baseOne>> heightOne;
if (baseOne <= 0 || heightOne <= 0)
{
cout << "Your value is invalid!!\nEnter again";
cin >> baseOne>> heightOne;
}
else
{
areaOne = (baseOne * heightOne) / 2;
cout << "Area of the first triangle is" << areaOne << endl;
}
cout << "What is the base and height of second triangle\n";
cin >> baseTwo>> heightTwo;
if (baseTwo <= 0 || heightTwo <= 0)
{
cout << "Your value is invalid!!\nEnter again";
cin >> baseTwo>> heightTwo;
}
else
{
areaTwo = (baseTwo * heightTwo) / 2;
cout << "Area of the second triangle is" << areaTwo << endl;
}
if (areaOne > areaTwo)
{
cout << "Area One is larger than area two\n";
}
else if (areaOne < areaTwo)
{
cout << "Area Two is larger than area one\n";
}
else
cout << "Magnitude of area one and area two is same ";
//system("pause");
return 0;
}
#包括
使用名称空间std;
int main()
{
双基一、基二、高度一、高度二、区域一、区域二、i1;
cout>baseOne>>heightOne;
如果(baseOne>heightOne);
}
其他的
{
区域一=(基础一*高度一)/2;
不能有两个高度;
如果(baseTwo>heightTwo);
}
其他的
{
区域二=(基底二*高度二)/2;
当你试图编译它时,你会得到什么错误?对于第二个三角形,我必须知道else areaOne==areaTwo;
?如果你得到else
块,它们必须相等,因为前两个If
条件检查
和这不是读取两个值的方式:cin>>baseOne,heightOne;
它应该是cin>>baseOne>>heightOne;
如果用户输入了无效的输入,您需要在打印错误后再次请求它。相反,您可以在else
块中再次请求它,即使他们给出了正确的输入。