黑马程序员技术交流社区

标题: C语言 if 和switch练习 [打印本页]

作者: jinlong129    时间: 2015-8-7 20:18
标题: C语言 if 和switch练习
本帖最后由 jinlong129 于 2015-8-8 15:35 编辑
  1. /*

  2. 输入一个整数score代表分数,根据分数输出等级(A-E)(用两种方式或以上)
  3. A:90~100
  4. B:80~89
  5. C:70~79
  6. D:60~69
  7. E:0~59

  8. */

  9. #include <stdio.h>

  10. int main()
  11. {
  12.    
  13.     //1.提示用户输入分数
  14.    
  15.     printf("请输入一个分数(0-100之间):\n");
  16.    
  17.     //2.接收用户输入的分数
  18.    
  19.     int score;
  20.     scanf("%d", &score);
  21.    
  22.     if (score < 0 || score > 100) {
  23.         printf("输入不合法!!! 请重新运行\n");
  24.         
  25.         return 0;
  26.     }

  27.    
  28.     //3.判断等级并输出对应的等级
  29.    
  30.         // 1.个人感觉性能最好
  31.     /*
  32.     int tmpe = score / 10;
  33.    
  34.     if (tmpe = 9 || tmpe = 10) {
  35.         printf("你的等级是A\n");  // 90~100
  36.     } else if (tmpe = 8) {
  37.         printf("你的等级是B\n"); // 80~89
  38.     } else if (tmpe = 7) {
  39.         printf("你的等级是C\n"); // 70~79
  40.     } else if (tmpe = 6) {
  41.         printf("你的等级是D\n"); // 60~69
  42.     } else  {
  43.         printf("你的等级是E\n"); // 0~69
  44.     }
  45.     */
  46.    
  47.     switch (score / 10) {
  48.         case 10:
  49.         case 9:
  50.             printf("你的等级是A\n");
  51.             break;
  52.         case 8:
  53.             printf("你的等级是B\n");
  54.             break;
  55.         case 7:
  56.             printf("你的等级是C\n");
  57.             break;
  58.         case 6:
  59.             printf("你的等级是D\n");
  60.             break;
  61.             
  62.         default:
  63.             printf("你的等级是E\n");
  64.             break;
  65.     }
  66.    
  67.     /*
  68.         //2.性能中等
  69.      
  70.     if (score >= 90 && score <= 100) {
  71.         printf("你的等级是A\n");  // 90~100
  72.     } else if (score >= 80) {
  73.         printf("你的等级是B\n"); // 80~89
  74.     } else if (score >= 70) {
  75.         printf("你的等级是C\n"); // 70~79
  76.     } else if (score >= 60) {
  77.         printf("你的等级是D\n"); // 60~69
  78.     } else  {
  79.         printf("你的等级是E\n"); // 0~69
  80.     }
  81.     */
  82.    
  83.    
  84.     /*
  85.         3.性能中等
  86.      
  87.     if (score >= 90 && score <= 100) {
  88.         printf("你的等级是A\n");  // 90~100
  89.     } else if (score >= 80 && score <= 89) {
  90.         printf("你的等级是B\n"); // 80~89
  91.     } else if (score >= 70 && score <= 79) {
  92.         printf("你的等级是C\n"); // 70~79
  93.     } else if (score >= 60 && score <= 69) {
  94.         printf("你的等级是D\n"); // 60~69
  95.     } else  {
  96.         printf("你的等级是E\n"); // 0~69
  97.     }
  98.     */
  99.      
  100.      
  101.     /*
  102.             //4.性能最差
  103.      
  104.     if (score >= 90 && score <= 100) {
  105.         printf("你的等级是A\n");  // 90~100
  106.     }
  107.     if (score >= 80 && score <= 89) {
  108.         printf("你的等级是B\n"); // 80~89
  109.     }
  110.     if (score >= 70 && score <= 79) {
  111.         printf("你的等级是C\n"); // 70~79
  112.     }
  113.     if (score >= 60 && score <= 69) {
  114.         printf("你的等级是D\n"); // 60~69
  115.     }
  116.     if (score >= 0 && score <= 59) {
  117.         printf("你的等级是E\n"); // 0~69
  118.     }
  119.     */
  120.    
  121.     return 0;
  122. }
复制代码


作者: Mal    时间: 2015-8-7 21:15
嘿嘿
  顶顶
作者: 墨琰    时间: 2015-8-7 22:10
顶你   楼主加油




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2