C++陣列運用 包含儲存人名、輸入體重、比較體重、及喜歡的食物。
#include <iostream>
using namespace std;
int main(){
cout << "歡迎來到喬治高中2年b班" << endl << "班上共有5人" << endl;
int x, y, a[5];
string n[5]{"小名","喬治","阿強","曉華","小美"};
string c[5]{"漢堡""可樂","冰淇淋","果汁","麵包"};
for (int i=0; i < 5; i++){
cout << "請輸入" << n[i] << "的體重:";
cin << a[i];
}
cout << "輸入你的座號及你最好的同學的座號:";
cin >> x >> y ;
cout << "你的體重是:" << a[x-1] << "\n" << n[y-1] << "的體重是:" << a[y-1] << endl;
cout << n[y-1] << "喜歡的食物是:" << c[y-1] << endl;
if(a[x-1] > a[y-1]){
cout << "你比" << n[y-1] << "重";
}else if(a[x-1] < a[y-1]){
cout << "你比" << n[y-1] << "輕";
}else{cout << "你跟" << n[y-1] << "一樣重";
}
return 0;
}
0 留言