成绩转换

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	int score;
	while(~scanf("%d", &score))
	{
		if(score > 100 || score < 0)
		{
			printf("Score is error!");
		}
		else {
			switch(score/10)
			{
				case 9 ... 10:
//				case 9:
					printf("A");
					break;
				case 8:
					printf("B");
					break;
				case 7:
					printf("C");
					break;
				case 6:
					printf("D");
					break;
				case 0 ... 5:
//				case 4:
//				case 3:
//				case 2:
//				case 1:
//				case 0:
					printf("E");
					break;
					
				default:
					printf("Score is error!");
			}
		}
		printf("\n");
	}
	
	return 0;
}

第一次遇见case 0 ... 5 这种写法, 这样写使得使用case写代码简化了不少, 0...5只能升序, 不能写成5...0

一定要注意, 不能直接写一个default来处理其他情况, 加入输入负数的话, switch中得到了0, 则会输出E, 所以要在输入是判断输入的数字是否在0~100之间

当然还有比较简洁的写法

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	char map[12] = "EEEEEEDCBAA";
	int score;
	
	while(~scanf("%d", &score))
	{
		if(score > 100 || score < 0)
		{
			printf("Score is error!\n");
		}
		else
		{
			printf("%c\n", map[score/10]);
		}
	}
	
	return 0;
} 

  

posted @ 2019-07-23 15:56  青衫客36  阅读(152)  评论(0编辑  收藏  举报