# C++ 程式有問題

#include<iostream>

using namespace std;

int main()

{

int bottom, hight, longer, wide, hb, lb, input, triangle, rectangle, trapezoid;

cout << "請輸入1.三角形 或 2.矩形 或 3.梯形";

cin >> input;

if (input >= 3)

cout << "請輸入上底";

cin >> hb;

cout << "請輸入下底";

cin >> lb;

cout << "請輸入高";

cin >> hight;

trapezoid = ((hb+lb)*hight)/2;

cout << "你的梯形面積為" << trapezoid;

else if (input >= 2)

cout << "請輸入長";

cin >> longer;

cout << "請輸入寬";

cin >> wide;

rectangle = longer*wide;

cout << "你的矩形面積為" << rectangle;

else (input >= 1)

cout << "請輸入底";

cin >> bottom;

cout << "請輸入高";

cin >> hight;

triangle = bottom*hight / 2;

cout << "你的三角形面積為" << triangle;

system("pause");

}

Rating

#include<iostream>

using namespace std;

int main() {

int bottom, hight, longer, wide, hb, lb, input, triangle, rectangle, trapezoid;

cout << "請輸入1.三角形 或 2.矩形 或 3.梯形";

cin >> input;

if (input >= 3){

cout << "請輸入上底";

cin >> hb;

cout << "請輸入下底";

cin >> lb;

cout << "請輸入高";

cin >> hight;

trapezoid = ((hb+lb)*hight)/2;

cout << "你的梯形面積為" << trapezoid;

}

else if (input >= 2) {

cout << "請輸入長";

cin >> longer;

cout << "請輸入寬";

cin >> wide;

rectangle = longer*wide;

cout << "你的矩形面積為" << rectangle;

}

else if(input >= 1) {

cout << "請輸入底";

cin >> bottom;

cout << "請輸入高";

cin >> hight;

triangle = bottom*hight / 2;

cout << "你的三角形面積為" << triangle;

}

system("pause");

}

------------------------

最後一個要用else if

不能用else

請養成縮排和加括號的習慣

這樣比較容易Debug

Source(s): 自己