黑马程序员技术交流社区

标题: if else习题练习中遇到的问题? [打印本页]

作者: 远人    时间: 2014-3-21 12:42
标题: if else习题练习中遇到的问题?
本帖最后由 远人 于 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。仔细查看了和老师之间的代码,也没错,编译器也没有报错,这是什么原因?
作者: Ranger    时间: 2014-3-21 12:47
我觉得可以这样写
#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;
}
作者: bubuatt    时间: 2014-3-21 12:51
你仔细看你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. }
复制代码

作者: Hi围城    时间: 2014-3-21 18:35
#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;
}
// 我的代码比你少哦,呵呵。
作者: 远人    时间: 2014-3-21 18:57
Hi围城 发表于 2014-3-21 18:35
#include
int main()
{

必须点赞,第一次有看到程序有心旷神怡的感觉!
作者: 远人    时间: 2014-3-21 18:59
远人 发表于 2014-3-21 18:57
必须点赞,第一次有看到程序有心旷神怡的感觉!

这方法收藏了,呵呵!
作者: 远人    时间: 2014-3-21 19:07
bubuatt 发表于 2014-3-21 12:51
你仔细看你else if (90 > score){
        printf("B\n");这句,只要是小于90的,这个条件就成立了,然后就 ...

恩,确实是这样,我这个地方出错就出错在我想遵循老师说的,数字写前面,变量写后面,避免将==写成=的错误,结果将输出结果写成这样了。
作者: 远人    时间: 2014-3-21 21:03
远人 发表于 2014-3-21 18:59
这方法收藏了,呵呵!

汗,这就是老师讲的switch方法
作者: 路默”    时间: 2014-3-21 23:13
#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;
}
作者: 黑马IT男    时间: 2014-3-22 10:47
本帖最后由 黑马IT男 于 2014-3-22 10:48 编辑

程序执行的过程中是一步步来的,当执行到 else if (90 > score){         printf("B\n");     }的时候,你输入的比如78,34啊都小于90噻,当然输出的是B




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