if语句实现问题 if a>b>c>d>e的时候作某事但当a,b,c,d,e各自为null时,各自不参与比较如b为null时,a>c>d>e 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 觉得就要事先过滤一下就可以if(a!=null && c!=null && d!=null && e!=null) { if(b!=null){ if(a>b>c>d>e){ ...... } } else { if(a>c>d>e){ ... } }} 等高手解答,是不是如果a和c同时为null,那就是比较b>d>e?楼上的可以没明白意思 if(b!=null){ if(a>b>c>d>e){ ...... } 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."); }}用下递归,以上代码仅供参考。 补充一下if a>b>c>d>e的时候作某事 但当a,b,c,d,e各自为null时,各自不参与比较 如b为null时,a>c>d>e 如b,c为null时,a>d>e 稍微改改不就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 判断下去的话,随着判断字符串的增加,复杂度增长的会很快。 如果 if 不满足a<b<c<d<e 的时候作某事 但当a,b,c,d,e各自为null时,各自不参与比较 又该如何实现 你去测试下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."); }} 菜鸟一枚,求各位前辈指点一二。 java Applet中图片未能显示 求各位大神帮忙解决一下 我的一小段程序,一运行就空指针错误,为什么 求 swing美化包和主题包 问一下在堆区中对象实例的方法被调用的机制,还是被放在栈中处理的么? 星期天我要期末考试-请各位好心人帮帮忙 Java不借助windows播放.asx后缀名的在线直播 请问怎么让生成的随机数不重复呢? 没人帮忙吗?我想用JAVA编一个像WINDOW中的画图软件,现在出现了如下问题,请各位老哥老姐帮帮忙! 面试题: 怎么把系统时间写入文件中? Java问题。大家帮帮看下
if(b!=null){
if(a>b>c>d>e){
......
}
}
else {
if(a>c>d>e){
...
}
}
}
if(a>b>c>d>e){
......
}
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.");
}
}用下递归,
以上代码仅供参考。
if a>b>c>d>e的时候作某事
但当a,b,c,d,e各自为null时,各自不参与比较
如b为null时,a>c>d>e
如b,c为null时,a>d>e
稍微改改不就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 判断下去的话,随着判断字符串的增加,复杂度增长的会很快。
但当a,b,c,d,e各自为null时,各自不参与比较
又该如何实现
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.");
}
}