admin管理员组

文章数量:1794759

c++中if语句的使用

c++中if语句的使用

形态一: #include

using namespace std;

int main(void) { int salary;

cout << "你月薪多少?"; cin >> salary; if (salary < 20000) { cout << "你是一个好人, 我还不想谈恋爱." << endl; } system("pause"); return 0;

} 形态二 #include

using namespace std;

int main(void) { int salary;

cout << "你月薪多少?" << endl; cin >> salary; if (salary < 20000) { cout << "你是一个好人, 我还不想谈恋爱." << endl; } else { cout << "我没有什么要求, 只要你对我好就行." << endl; } system("pause"); return 0;

} 形态三: #include #include #include <stdio.h>

using namespace std;

int main(void) { int salary; string houseRet; //是否有房 string carRet; //是否有车

cout << "你的月薪是多少?" << endl; cin >> salary; cout << "你有房吗?" << endl; cin >> houseRet; cout << "你有车吗?" << endl; cin >> carRet; if (salary < 20000) { cout << "你是一个好人, 我现在还不想谈恋爱." << endl; } else if (houseRet == "有") { cout << "我其实没有什么要求,只要你对我好." << endl; } else if (carRet == "有") { cout << "还不错哦, 以后再联系." << endl; } else { cout << "有缘再见." << endl; } system("pause"); return 0;

} 终极形态:嵌套循环 #include #include <windows.h>

using namespace std; int main(void) { int x, y, z;

cout << "请输入3个整数: " << endl; cin >> x >> y >> z; if (x > y) { if (x > z) { cout << "最大值是: " << x << endl; } else { cout << "最大值是: " << z << endl; } } else { if (y > z) { cout << "最大值是: " << y << endl; } else { cout << "最大值是: " << z << endl; } } system("pause"); return 0;

}

本文标签: 语句