asked in 電腦與網際網路程式設計 · 1 decade ago

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");

}

3 Answers

Rating
  • 1 decade ago
    Favorite Answer

    #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): 自己
  • 1 decade ago

    謝謝你的提醒

    我會改進的

  • 1 decade ago

    當input 為4時會被判斷成項目3的工作

Still have questions? Get your answers by asking now.