Created
January 4, 2018 16:16
-
-
Save goodjack/4b9b50faf59b6de9b7a871d5a57ee300 to your computer and use it in GitHub Desktop.
20180105 課堂範例:function、switch 和 三元運算式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// 先宣告一個 function(函數、函式) | |
// 第一個 int 代表回到上一層 function 時,return 值的型態 | |
// 第二個 int 代表代進去的值的型態 | |
int sum(int, int); | |
int main() { | |
int num1, num2; | |
cout << "請輸入第一個數:"; | |
cin >> num1; | |
cout << "請輸入第二個數:"; | |
cin >> num2; | |
cout << "[方法一] 總和為:" << num1 + num2 << endl; | |
cout << "[方法二] " << sum(num1, num2) << endl; | |
cout << "程式結束。" << endl; | |
return 0; // 因為有 return 0,所以 main 的前面是 int。 | |
} | |
int sum(int numA,int numB) { | |
cout << "現在已經進來 sum function" << endl; | |
// 注意這裡是 numA, numB,不是 num1, num2 | |
cout << "numA = " << numA << endl; | |
cout << "numB = " << numB << endl; | |
cout << "準備 return,相加的值是由 main 的 cout 印出結果" << endl; | |
return numA + numB; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// void 代表沒有要回傳值 | |
void standard_edition(); | |
void downloadable_content(); | |
int main() { | |
char gameVersion; | |
cout << "請輸入你買的遊戲版本\n(s: 標準版, d: 含有 DLC 的完整版):"; | |
cin >> gameVersion; | |
switch(gameVersion) { | |
case 'd': | |
// DLC 的內容部分 | |
downloadable_content(); | |
// 這裡沒放 break 的話,也會繼續執行下面的 case 的內容 | |
// break; | |
case 's': | |
// 標準版的內容部分 | |
standard_edition(); | |
break; | |
default: | |
cout << "沒有你輸入的版本" << endl; | |
break; | |
} | |
cout << "程式結束" << endl; | |
return 0; | |
} | |
void standard_edition() { | |
cout << "這裡是標準版的遊戲內容" << endl; | |
return; // 可寫可不寫 | |
cout << "遇到 return 就會回去原本這個 function 被呼叫的地方,所以這行不會被印出"; | |
} | |
void downloadable_content() { | |
cout << "這裡是 DLC 的遊戲內容" << endl; | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
// 布林型態:只能是 true 或 false | |
bool basketballTeam = true; | |
if (basketballTeam) { | |
cout << "球隊有在班上" << endl; | |
} else { | |
cout << "球隊不在班上" << endl; | |
} | |
int teamSize = 20, classSize = 30; | |
cout << "球隊 " << teamSize << " 人" << endl; | |
cout << "班上原本 " << classSize << " 人" << endl; | |
int peopleInClassroom = (basketballTeam) ? (teamSize + classSize) : classSize; | |
cout << "教室現在有 " << peopleInClassroom << " 人" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment