if a>b>c>d>e的时候作某事
但当a,b,c,d,e各自为null时,各自不参与比较
如b为null时,a>c>d>e

解决方案 »

  1.   

    觉得就要事先过滤一下就可以if(a!=null && c!=null && d!=null && e!=null) {
       if(b!=null){
          if(a>b>c>d>e){
           ......
          }
       }
       else {
          if(a>c>d>e){
            ...
          }
       }
    }
      

  2.   

    等高手解答,是不是如果a和c同时为null,那就是比较b>d>e?楼上的可以没明白意思
      

  3.   

       if(b!=null){
          if(a>b>c>d>e){
           ......
          }
      

  4.   


    import java.util.ArrayList;
    import java.util.List;public class Simple {
    static Integer a, b, c, d, e; public static void main(String args[]) {
    setValue();
    Integer[] all = { a, b, c, d, e }; ArrayList<Integer> list = new ArrayList<Integer>(); //
    for (Integer i : all)
    if (i != null)
    list.add(i); if (canDoSomething(list)) {
    toDoSomething();
    }
    } //為a, b, c, d, e 賦zhi
    static void setValue() {
    a = 2;
    b = 1;
    c = null;
    d = 0;
    e = null;
    } //判斷是否符合條件
    static boolean canDoSomething(List<Integer> list) {
    int i = 0;
    int size = list.size(); //不為null的大于等于2個的時候
    if (size > 1) {
    //如果一個數大于它后面的數
    if (isBiger(list.get(i), list.get(i + 1))) {
    i++;
    //判斷是否還有不為null的數
    if (i < size - 1) {
    //去掉之前判斷過的數,進行遞歸
    list.remove(i - 1);
    return canDoSomething(list);
    } else {
    return true;
    }
    } else
    return false;
    }
    //如果不為null的 少與2個,則直接返回true
    else
    return true;
    } static boolean isBiger(Integer i, Integer j) {
    return i >= j ? true : false;
    } static void toDoSomething() {
    System.out.println("I can do it.");
    }
    }用下递归,
    以上代码仅供参考。
      

  5.   

    补充一下
    if a>b>c>d>e的时候作某事 
    但当a,b,c,d,e各自为null时,各自不参与比较 
    如b为null时,a>c>d>e 
    如b,c为null时,a>d>e 
      

  6.   


    稍微改改不就OK了吗!import java.util.ArrayList;
    import java.util.List;public class Simple {
    static String a, b, c, d, e; public static void main(String args[]) {
    setValue();
    String[] all = { a, b, c, d, e }; ArrayList<String> list = new ArrayList<String>(); //
    for (String i : all)
    if (i != null)
    list.add(i); if (canDoSomething(list)) {
    toDoSomething();
    }
    } //為a, b, c, d, e 賦zhi
    static void setValue() {
    a = "dd";
    b = "cc";
    c = null;
    d = "cc";
    e = null;
    } //判斷是否符合條件
    static boolean canDoSomething(List<String> list) {
    int i = 0;
    int size = list.size(); //不為null的大于等于2個的時候
    if (size > 1) {
    //如果一個數大于它后面的數
    if (isBiger(list.get(i), list.get(i + 1))) {
    i++;
    //判斷是否還有不為null的數
    if (i < size - 1) {
    //去掉之前判斷過的數,進行遞歸
    list.remove(i - 1);
    return canDoSomething(list);
    } else {
    return true;
    }
    } else
    return false;
    }
    //如果不為null的 少與2個,則直接返回true
    else
    return true;
    } static boolean isBiger(String i, String j) {
    return i.compareTo(j) >= 0 ? true : false;
    } static void toDoSomething() {
    System.out.println("I can do it.");
    }
    }
    用这个递归的方法有个好处,就是如果你要更多的String相比较,
    比如,a,b,c,d,e,f,g,h,i等字符串,
    做法还是一样。
    如果你要用if else 判断下去的话,随着判断字符串的增加,复杂度增长的会很快。
      

  7.   

    如果 if 不满足a<b<c<d<e 的时候作某事 
    但当a,b,c,d,e各自为null时,各自不参与比较 
    又该如何实现
      

  8.   

    你去测试下import java.util.ArrayList;
    import java.util.List;public class Simple {
    static String a, b, c, d, e; public static void main(String args[]) {
    setValue();
    String[] all = { a, b, c, d, e }; ArrayList<String> list = new ArrayList<String>(); //
    for (String i : all)
    if (i != null)
    list.add(i); if (!canDoSomething(list)) {//这里加个!
    toDoSomething();
    }
    } //為a, b, c, d, e 賦zhi
    static void setValue() {
    a = "dd";
    b = "ee";
    c = null;
    d = "cc";//ff
    e = null;
    } //这里判断,如果a<b<c<d<e返回true。所以在上面调用的时候前面加!
    static boolean canDoSomething(List<String> list) {
    int i = 0;
    int size = list.size(); //不為null的大于等于2個的時候
    if (size > 1) {
    //如果一個數小于它后面的數
    if (isNotBigger(list.get(i), list.get(i + 1))) {
    i++;
    //判斷是否還有不為null的數
    if (i < size - 1) {
    //去掉之前判斷過的數,進行遞歸
    list.remove(i - 1);
    return canDoSomething(list);
    } else {
    return true;
    }
    } else
    return false;
    }
    //如果不為null的 少與2個,則直接返回true
    else
    return true;
    } static boolean isBigger(String i, String j) {
    return i.compareTo(j) >= 0 ? true : false;
    }

    static boolean isNotBigger(String i, String j) {//追加的方法
    return i.compareTo(j) <= 0 ? true : false;
    } static void toDoSomething() {
    System.out.println("I can do it.");
    }
    }