1 // 29-if语句.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <climits>
7 #include <string>
8 #include <array>
9 #include <math.h>
10 using namespace std;
11
12 int main()
13 {
14 int hp = 0;
15 if (hp <= 0)
16 {
17 cout << "游戏结束" << endl;
18 }
19 //还可以这么写
20 if (hp <= 0)
21 cout << "游戏结束" << endl; //这么写只能默认第一条语句是body
22
23 //if...else语句
24 if (hp <= 0)
25 {
26 cout << "游戏结束" << endl;
27 }
28 else
29 {
30 cout << "游戏继续" << endl;
31 }
32
33 //年龄保护游戏
34 int age = 60;
35 if (age < 18)
36 {
37 cout << "你可以玩3个小时" << endl;
38 }
39 else
40 {
41 if (age < 50)
42 {
43 cout << "你可以玩10个小时" << endl;
44 }
45 else
46 {
47 cout << "你可以玩2个小时" << endl;
48 }
49 }
50
51 //if...else if...else
52 if (age < 18)
53 {
54 cout << "你可以玩3个小时" << endl;
55 }
56 else if (age < 50)
57 {
58 cout << "你可以玩10个小时" << endl;
59 }
60 else if (age < 80)
61 {
62 cout << "你可以玩2个小时" << endl;
63 }
64 else
65 {
66 cout << "你不能玩这个游戏" << endl;
67 }
68
69 int t;
70 cin >> t;
71 return 0;
72 }