Table Driven(表驱动)方法实例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Table Driven(表驱动)方法实例

Table Driven(表驱动)方法实例

 2015/1/7 0:13:57  bijian1013  程序员俱乐部  我要评论(0)
  • 摘要:/***驾驶人年龄段*保险行业,会对驾驶人的年龄做年龄段的区分判断*驾驶人年龄段:01-[18,25);02-[25,30);03-[30-35);04-[35,40);05-[40,45);06-[45,50);07-[50-55);08-[55,+∞)*/publicclassAgePeriodTest{//if...else...方法/*publicstaticStringgetAgePeriod(intage){StringagePeriod="";if(age>55)
  • 标签:方法 实例
class="java" name="code">/**
 * 驾驶人年龄段
 * 保险行业,会对驾驶人的年龄做年龄段的区分判断
 * 驾驶人年龄段:01-[18,25);02-[25,30);03-[30-35);04-[35,40);05-[40,45);06-[45,50);07-[50-55);08-[55,+∞)
 */
public class AgePeriodTest {

    //if...else...方法
    /*
    public static String getAgePeriod(int age) {
        
        String agePeriod = "";
        if (age > 55) {
            agePeriod = "08";
        } else if (age >= 50) {
            agePeriod = "07";
        } else if (age >= 45) {
            agePeriod = "06";
        } else if (age >= 40) {
            agePeriod = "05";
        } else if (age >= 35) {
            agePeriod = "04";
        } else if (age >= 30) {
            agePeriod = "03";
        } else if (age >= 25) {
            agePeriod = "02";
        } else if (age >= 18) {
            agePeriod = "01";
        }
        return agePeriod;
    }
    */
    
    //Table Driven(表驱动)方法
    enum AgePeriod {
        A(55, "08"),   
        B(50, "07"),   
        C(45, "06"),
        D(40, "05"),   
        E(35, "04"),   
        F(30, "03"),   
        G(25, "02"),   
        H(18, "01");    
        
        private int age;   
        private String agePeriod;     
        
        AgePeriod(int age, String agePeriod) {     
            this.age= age;     
            this.agePeriod = agePeriod;   
        } 
    }
    
    public static String getAgePeriod(int age) {
        for (AgePeriod agePeriod : AgePeriod.values()) {
            if (age >= agePeriod.age) {
                return agePeriod.agePeriod;
            }
        }
        return "";
    }
    
    public static void main(String[] args) {
        String res = getAgePeriod(1);
        System.out.println("res:-" + res);
        res = getAgePeriod(18);
        System.out.println("res:01-" + res);
        res = getAgePeriod(24);
        System.out.println("res:01-" + res);
        res = getAgePeriod(25);
        System.out.println("res:02-" + res);
        res = getAgePeriod(29);
        System.out.println("res:02-" + res);
        res = getAgePeriod(30);
        System.out.println("res:03-" + res);
        res = getAgePeriod(34);
        System.out.println("res:03-" + res);
        res = getAgePeriod(35);
        System.out.println("res:04-" + res);
        res = getAgePeriod(39);
        System.out.println("res:04-" + res);
        res = getAgePeriod(40);
        System.out.println("res:05-" + res);
        res = getAgePeriod(44);
        System.out.println("res:05-" + res);
        res = getAgePeriod(45);
        System.out.println("res:06-" + res);
        res = getAgePeriod(49);
        System.out.println("res:06-" + res);
        res = getAgePeriod(50);
        System.out.println("res:07-" + res);
        res = getAgePeriod(54);
        System.out.println("res:07-" + res);
        res = getAgePeriod(55);
        System.out.println("res:08-" + res);
        res = getAgePeriod(59);
        System.out.println("res:08-" + res);
        res = getAgePeriod(60);
        System.out.println("res:08-" + res);
        res = getAgePeriod(100);
        System.out.println("res:08-" + res);
    }
}
运行结果:
res:-
res:01-01
res:01-01
res:02-02
res:02-02
res:03-03
res:03-03
res:04-04
res:04-04
res:05-05
res:05-05
res:06-06
res:06-06
res:07-07
res:07-07
res:08-07
res:08-08
res:08-08
res:08-08
上一篇: 如何通过struts的标签调用java静态的方法? 下一篇: 没有下一篇了!
发表评论
用户名: 匿名