给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。 90分以及90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。
范围断层的代码为:
// An highlighted block Scanner sc=new Scanner(System.in); int x=sc.nextInt(); if(x>=90&&x<=100){ System.out.println('A'); }else if(x>=80){ System.out.println('B'); }else if(x>=70&&x<80){ System.out.println('C'); }else if(x>=60&&x<70){ System.out.println('D'); }else{ System.out.println('E');等诸如此类因为前后范围断层或不连贯导致代码运行没有错误,但结果是错误的情况。
正确的为:
Scanner sc=new Scanner(System.in); int x=sc.nextInt(); if(x>=90){ System.out.println('A'); }else if(x>=80){ System.out.println('B'); }else if(x>=70&&x<80){ System.out.println('C'); }else if(x>=60&&x<70){ System.out.println('D'); }else{ System.out.println('E'); }或
Scanner sc=new Scanner(System.in); int x=sc.nextInt(); if(x<=100){ if(x>=90){ System.out.println('A'); }else if(x>=80){ System.out.println('B'); }else if(x>=70&&x<80){ System.out.println('C'); }else if(x>=60&&x<70){ System.out.println('D'); }else{ System.out.println('E'); } }之类