黑马程序员技术交流社区

标题: 《哈尔滨校区》神题…………四版本变成 [打印本页]

作者: 面嘛嘛    时间: 2015-11-27 22:58
标题: 《哈尔滨校区》神题…………四版本变成
本帖最后由 张研老师 于 2015-11-29 23:25 编辑

考试成绩分等级。
        90~100        A等。
        80-89        B等。
        70-79        C等。
        60-69        D等。
        60以下        E等。
请根据给定成绩,输出对应的等级。
第一版
import java.util.Scanner;
class As {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out. println("请输入一个数值");
                int a = sc.nextInt();
                if (a >= 90 && a <= 100 ) {
                        System.out.println("优");
                }else if (a >= 80 && a <= 89) {
                 System.out.println("良");
                }else if (a >= 70 && a <= 79) {
                 System.out.println("中");
                }else if (a >= 60 && a <= 69) {
                 System.out.println("及");
                }else {
                 System.out.println("差");
                }
}}
第二版
import java.util.Scanner;
class As2 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数值");
                int a = sc.nextInt();
                if (a > 100) {
                        System.out.println("成绩不存在");
                }else if (a >= 90) {
                        System.out.println("优");
                }else if (a >= 80) {
                        System.out.println("良");
                }else if (a >= 70) {
                        System.out.println("中");
                }else if (a >= 60) {
                        System.out.println("及");
                }else if (a >= 0) {
                        System.out.println("差");
            }else {
                        System.out.println("成绩不存在");                }
        }
}
第三版
import java.util.Scanner;
class As2 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个成绩");
                int a = sc.nextInt();
                if (a >= 0 && a <= 100) {
                        switch (a / 10) {
                            case 10:
                                System.out.println("优");
                                break;
                                case 9:
                                System.out.println("优");
                                break;
                                case 8:
                                System.out.println("良");
                                break;
                                case 7:
                                System.out.println("中");
                                break;
                                case 6:
                                System.out.println("及");
                                break;
                                default :
                                System.out.println("差");
                                break;
                        }
                }        else {
                                System.out.println("成绩不存在");               
                        }
        }
}
第四版
import java.util.Scanner;
class As3 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数值");
                int a = sc.nextInt();
                String str = "";
                if (a >= 90 && a <= 100 ) {
                        str="优";
                }else if (a >= 80 && a <= 89) {
                        str="良";
                }else if (a >= 70 && a <= 79) {
                        str="中";
                }else if (a >= 60 && a <= 69) {
                        str="及";
                }else {
                        str="差";
                }
                System.out.println(str);
        }
}
14:switch语句的格式?针对格式的解释?以及注意事项?
格式:
                                switch(表达式) {
                                        case 值1:
                                                语句体1;
                                                break;
                                        case 值2:
                                                语句体2;
                                                break;
                                        .....
                                        default:
                                                语句体3;
                                                break;        
                                }
执行流程:
                                先计算表达式的值,然后从第一个case进行匹配,能匹配上就执行对应的语句体,
                                如果所有的case都匹配不上,就执行default中的内容。
                        注意:
                                表达式可以放什么?
                                        byte,short,char,int,JDK1.5以后支持枚举,JDK1.7以后支持字符串(String)。
15:看程序,分析下面程序的结果:
int x = 2,y=3;

switch(x)
{
        default:
                y++;
        case 3:
                y++;
                break;
        case 4:
                y++;
}

System.out.println("y="+y);
y=5

16:根据输入的值,判断是星期几。(分别用if语句和switch语句实现)
        输入:1         
                输出:星期1               
                class  Qwe{
        public static void main(String[] args) {
                int a = 1;
                String str = "";
                if (a < 1 || a > 7) {
                        str = ("星期不存在");
                        }else if (a == 1) {
                                str = ("星期一");
                        }else if (a == 2) {
                                str = ("星期二");
                        }else if (a == 3) {
                                str = ("星期三");
                        }else if (a == 4) {
                                str = ("星期四");
                        }else if (a == 5) {
                                str = ("星期五");
                        }else if (a == 6) {
                                str = ("星期六");
                        }
                        else {
                           str = ("星期天");        
                }
                System.out.println(str);
        }
}
class Qwe {
        public static void main(String[] args) {
                int a = 10;
                String str = "";
                switch (a) {
                    case 1:
                                str = "星期一";
                        break;
                         case 2:
                                str = "星期二";
                        break;
                          case 3:
                                str = "星期三";
                        break;
                           case 4:
                                str = "星四";
                        break;
                            case 5:
                                str = "星期五";
                        break;
                                case 6:
                                str = "星期六";
                        break;
                                case 7:
                                str = "星期天";
                        break;
                                default :
                                str = "星期不存在";

                }
                System.out.println(str);
        }
}

作者: 流水落花    时间: 2015-11-28 22:49
无聊了,,去敲代码
作者: hansnowqiang    时间: 2015-11-29 00:24
同问,什么鬼?




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