小弟剛開始學C++程式,有個作業試了很久一直跑不出來,不知問題在哪,請高手幫我改程式,並告訴我哪裡錯了,謝謝
題目:
Enter your age: -3
Age must be between 1 and 120- enter again: 125
Age must be between 1 and 120- enter again: 0
…….etc.
Age must be between 1 and 120- enter again: 22
You are an adult.
我所寫的程式:
#include
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
while (age <= 0 || age > 120)
{
cout << "Age must be between 1 and 120 - enter again: ";
cin >> age;
if (age <= 0 || age > 120)
{
cout << "Age must be between 1 and 120 - enter again: ";
cin >> age;
}
else
{
if (age <= 17)
{
cout << "You are not an adult." << endl;
}
else
{
cout << "You are an adult." << endl;
}
break;
}
}
}
2006-10-14 22:44:06 · 2 個解答 · 發問者 Jianjou 3 in 電腦與網際網路 ➔ 程式設計
可否請您依我原本的程式來改,因為老師要求用if, else, while來寫,而且你的程式寫太好,老師ㄧ看就知道不是我寫的,謝謝
2006-10-15 15:49:38 · update #1
//Power by Microsoft Visual Studio 2005//可以使用 Dev-C++ 編譯此程式#include
2006-10-15 04:29:49 補充:
你的程式錯誤在於……因為程式的重要訊息全部在迴圈內,在你設計的程序上,必須先輸入超出範圍的數值,才能進入程式的迴圈內,讓程式判斷輸入的數值。
2006-10-15 20:11:01 補充:
你可以修改成下方的程式碼先定 age 為錯誤的值進入迴圈後,再輸入正確數值int age= -1;while (age <= 0 || age > 120){cout << "Enter your age: ";cin >> age;…………//略}
2006-10-15 00:13:17 · answer #1 · answered by Big_John-tw 7 · 0⤊ 0⤋
http://cetus.cs.tku.edu.tw/~u92510590/age.htm
用dev-c++ 編譯器
我這樣寫更簡單明遼 , 應該很容易懂 , 如果覺得不錯有點分數^^
如果有不明白地方請再發問
謝謝
2006-10-16 02:21:23 補充:
耗呆小綿羊 大大你好bool CHECK_AGE(int AGE){ return (AGE<0||AGE>120)?true:false;} AGE<0 沒考慮到 age 0 應該是 AGE <= 0
2006-10-15 21:32:30 · answer #2 · answered by ? 1 · 0⤊ 0⤋