[Day-13] 条件运算子以及switch小练习

这次就要来练习上次学习的switch
这边我想了个题目来练习
那现在就开始练习程序码吧!

题目:利用哈利波特中的普通巫术等级测验,将成绩输入後转换为普通巫术等级测验。

Input:一个整数
Output:
100的分数为「O」杰出
90~99的分数为「E」超乎期待
80~89的分数为「A」合格
70~79的分数为「P」不佳
60~69的分数为「D」糟糕
其余分数为「T」山怪

程序码:

#include <iostream>
using namespace std;

int main(void){
    int score;
    cout << "Please input your score:";
    cin >> score;
    score = score/10;
    switch(score){
        case 10:
            cout << "Your level is " 
				 << "O" << '\n';
            break;
        case 9:
            cout << "Your level is " 
			     << "E" << '\n';
            break;
        case 8:
            cout << "Your level is "  
				 << "A" << '\n';
            break;
        case 7:
            cout << "Your level is " 
				 << "P" << '\n';
            break;
        case 6:
            cout << "Your level is " 
				 << "D" << '\n';
            break;
        default:
            cout << "Your level is " 
				 << "T" << '\n';
            break;
    }
}

执行结果:
100

Please input your score:100
Your level is O
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

96

Please input your score:96
Your level is E
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

87

Please input your score:87
Your level is A
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

78

Please input your score:78
Your level is P
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

66

Please input your score:66
Your level is D
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

43

Please input your score:43
Your level is T
--------------------------------
Process exited after 0.08787 seconds with return value 0
请按任意键继续...

程序码解释:

宣告变数score
利用cin将键盘输入的成绩存放到变数score
再让变数score除以10
载入switch利用变数score找到相对应的case
并将结果显示在命令提示字元上
执行完毕後break跳出回圈
结束程序

举例来说↓
输入100
则会到switch的case 10执行
命令提示字元显示「O」
Break跳出回圈

输入96
则会到switch的case 9执行
命令提示字元显示「E」
Break跳出回圈

输入87
则会到switch的case 8执行
命令提示字元显示「A」
Break跳出回圈

例如输入78
则会到switch的case 7执行
命令提示字元显示「P」
Break跳出回圈

例如输入66
则会到switch的case 6执行
命令提示字元显示「D」
Break跳出回圈

例如输入43
则会到switch的default执行
命令提示字元显示「T」
Break跳出回圈

以上就是我今天的练习~
/images/emoticon/emoticon08.gif

-End-

参考文章:http://www.crown.com.tw/harrypotter/375331_page04.html


<<:  D7 - 你不知道Combo: 第二主菜 Execution Context

>>:  [SQL] 读 XML 格式文件写入 SQL table

近似最短路径 (6)

11.6 距离标签 Distance Labellings 最短距离资料结构 Distance Or...

Day13 Sass篇-什麽是变数?

大家好,我是乌木白,今天要介绍的是 Sass 里的变数! 变数是什麽? 变数按照字面上来看,就是一...

DAY04 浅谈资料科学与Machine Learning

一、何谓资料科学 资料科学白话来讲,就是透过资料数据来解决问题的一门科学,我们在得知客户的需求、环境...

第47天-学习一次性排程工作 at

今天进度 : 鸟哥私房菜 - 第十五章、例行性工作排程(crontab) 使用 systemctl ...

DAY 01 动机与LINE Messaging API 介绍

动机 小弟在超商打工,老板有多间店的群组要管理,接收发讯息都要人工处理,所以想要做个群组机器人减少人...