A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 远人 中级黑马   /  2014-3-21 12:42  /  2296 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 远人 于 2014-3-22 13:01 编辑

// 3.输入一个整数score代表分数,根据分数输出等级(A-E)(用两种方式)

//第三种方法:性能最好
#include <stdio.h>

int main(int argc, const char * argv[])
{
    printf("输入一个分数score==");
    int score;
    scanf("%d",&score);
   
    if (100 >= score && 90 <= score) {
        printf("A\n");
    }
    else if (90 > score){
        printf("B\n");
    }
    else if (80 > score){
        printf("C\n");
    }
    else if (70 > score){
        printf("D\n");
    }
    else if (60 > score && 0 <= score){
        printf("E\n");
    }
    else{
        printf("这个数字超出分数范围\n");
    }
   
    return 0;
}

这是老师上课讲的习题,按照老师讲的,这是最优化的方法,但是输出结果有问题:100~90之间显示答案是A;89~80之间答案是B;但是输入79之后的数字答案都是B,比如输入数字54,答案是B。仔细查看了和老师之间的代码,也没错,编译器也没有报错,这是什么原因?

评分

参与人数 1技术分 +1 收起 理由
jing迪 + 1

查看全部评分

9 个回复

倒序浏览
我觉得可以这样写
#include <stdio.h>

int main(int argc, const char * argv[])
{
    printf("输入一个分数score==");
    int score;
    scanf("%d",&score);
   
    if (100 >= score && 90 <= score) {
        printf("A\n");
    }
    else if (score>=80&&90 > score){
        printf("B\n");
    }
    else if (score>=70&&9080 > score){
        printf("C\n");
    }
    else if (70 > score){
        printf("D\n");
    }
    else if (60 > score && 0 <= score){
        printf("E\n");
    }
    else{
        printf("这个数字超出分数范围\n");
    }
   
    return 0;
}
回复 使用道具 举报
你仔细看你else if (90 > score){
        printf("B\n");这句,只要是小于90的,这个条件就成立了,然后就回输出B,所以这个循环控制有问题,
就是第一句如果大于90就会输出A,条件不成立,就是少于90,就会往下执行,要输出B就是要大于80..然后以此类推,希望帮助下你

可以这样修改
  1. #include <stdio.h>

  2. void main()
  3. {
  4.     int score; //定义分数
  5.         printf("请输入分数:0~100\n"); //输出语句
  6.         scanf("%d",&score);//接受分数并存放入score中
  7.         if(score>=90&&score<=100) //条件判断
  8.                 putchar('A');
  9.         else if(score>=80)
  10.                 putchar('B');
  11.         else if(score>=70)
  12.                 putchar('C');
  13.         else if(score>=60)
  14.                 putchar('D');
  15.         else
  16.                 putchar('E');

  17. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
jing迪 + 1

查看全部评分

回复 使用道具 举报
#include <stdio.h>
int main()
{
    // 提示用户输入数据
    printf("请输入一个分数:\n");
    // 定义变量存储数据
    int score = 0;
    // 接收数据
    scanf("%d", &score);
    // 判断等级,输出
    switch (score/10) {
        case 10:
        case 9:
            printf("A");
            break;
        case 8:
            printf("B");
            break;
        case 7:
            printf("C");
            break;
        case 6:
            printf("D");
            break;
        default:
            printf("E");
            break;
    }
    return 0;
}
// 我的代码比你少哦,呵呵。

评分

参与人数 1技术分 +1 收起 理由
jing迪 + 1

查看全部评分

回复 使用道具 举报

必须点赞,第一次有看到程序有心旷神怡的感觉!
回复 使用道具 举报
远人 发表于 2014-3-21 18:57
必须点赞,第一次有看到程序有心旷神怡的感觉!

这方法收藏了,呵呵!
回复 使用道具 举报
远人 中级黑马 2014-3-21 19:07:25
7#
bubuatt 发表于 2014-3-21 12:51
你仔细看你else if (90 > score){
        printf("B\n");这句,只要是小于90的,这个条件就成立了,然后就 ...

恩,确实是这样,我这个地方出错就出错在我想遵循老师说的,数字写前面,变量写后面,避免将==写成=的错误,结果将输出结果写成这样了。
回复 使用道具 举报
远人 中级黑马 2014-3-21 21:03:43
8#
远人 发表于 2014-3-21 18:59
这方法收藏了,呵呵!

汗,这就是老师讲的switch方法
回复 使用道具 举报
#include <stdio.h>

int main(int argc, const char * argv[])
{
    printf("输入一个分数score==");
    int score;
    scanf("%d",&score);
   
    if (100 >= score && 90 <= score) {
        printf("A\n");
    }
    else if (90 > score){                 《==运行到这里判断数值小于90  《符合》,因此下方的else if 将不再执行               
        printf("B\n");                                可改为score>80
    }
    else if (80 > score){          《==依次score>70
        printf("C\n");
    }
    else if (70 > score){                       score>60
        printf("D\n");
    }
    else if (60 > score && 0 <= score){
        printf("E\n");
    }
    else{
        printf("这个数字超出分数范围\n");
    }
   
    return 0;
}

评分

参与人数 1技术分 +1 收起 理由
jing迪 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 黑马IT男 于 2014-3-22 10:48 编辑

程序执行的过程中是一步步来的,当执行到 else if (90 > score){         printf("B\n");     }的时候,你输入的比如78,34啊都小于90噻,当然输出的是B
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马