編輯:如何循環的聲明,C++
這裏是工作的,但如果你輸入什麼,但一個數字,程序進入一個無限循環的代碼。
#include <iostream>
using namespace std;
int main()
{
int age; // Declare age as integer
do
{
cout << "Enter Age: "; // Asks the user to enter their age
cin >> age; // Takes input as age
if (age < 18) // If age is less than 18
cout << "Sorry, you cannot use this program" << endl;
} while (age < 18);
cout << "Welcome";
system("pause");
return 0;
}
原貼:
我最近開始學習C++,想知道最好辦法環路Enter Age
程序,使用戶能夠重新進入他們的年齡。
#include <iostream>
using namespace std;
int main()
{
int age; // Declare age as integer
cout << "Enter Age: "; // Asks the user to enter their age
cin >> age; // Takes input as age
if (age < 18) // If age is less than 18
{
cout << "Sorry, you cannot use this program" << endl;
}
else if (age >= 18) // If age is greater than 18
{
cout << "Welcome" << endl;
}
system("pause");
return 0;
}
你是什麼意思*最有效*?你肯定不會試圖將人體納入一個循環的納秒? –
「最好」 - 取決於你的意思是「最好的」; 「最有效的」 - 你應該在特定情況下測量每一個。 –
@EdChum謝謝。不,我只想知道什麼效率最高,以便我可以在未來的更大規模的計劃中使用它。 – CypherPsyco