判断三个变量只有一个不是空 判断三个参数a,b,c ,只有一个不为空返回true ,其他情况都返回false.boolean test(string a,string b,Long c){ } 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没办法,用逻辑图看,只能一个一个的枚举boolean test(string a,string b,Long c){ return (a!=null && b == null && c == null) || (a==null && b != null && c == null) || (a=null && b == null && c != null);} public boolean test(String a,String b,Long c) { boolean [] flag = new boolean[3];//保存是否为空的结果 flag[0] = (a == null) ? true:false; flag[1] = (b == null) ? true:false; flag[2] = (c == null) ? true:false; int count = 0;//计数,为空的参数个数 for(int i=0; i<flag.length; i++) { if(flag[i]) count ++; } if(count == 2) return true; else return false;} boolean test(string a,string b,Long c){ int i=0; if(a!=null) i++; if(b!=null) i++; if(c!=null) i++;return i==1?true:false;} boolean test(string a,string b,Long c){ if(!a==null) return true ;if(!a==null) return true ;if(!a==null) return true ;} 3楼不错,不过最后一个return写复杂了,return i==1; 就可以了。 flag[0] = (a == null) ? true:false;复杂了直接flag[0] = (a == null);即可 综合前面的:return ( a==null?1:0 + b==null?1:0 + c==null?1:0 ) ==1; 只有一个不为空,上面的反了:return ( a==null?0:1 + b==null?0:1 + c==null?0:1 ) ==1;或return ( a!=null?1:0 + b!=null?1:0 + c!=null?1:0 ) ==1;或return ( a==null?1:0 + b==null?1:0 + c==null?1:0 ) ==2;y? System.out.println((a == null) ^ (b == null) ^ (c == null)); 写反了return (a != null) ^ (b != null) ^ (c != null); 上面的情况不包括 String s="";这个和null是不一样的额!! 学习了不过 (a != null) ^ (b != null) ^ (c != null) 好像不对,如果 a,b,c都不是null, 还是返回 true给个通用解法,即不受3个参数限制的代码boolean test(int maxNotEmpty, Object... args){ for(Object str: args){ if (str != null && (String)str.trim().length() > 0){ maxNotEmpty--; if(maxNotEmpty < 0) return false; } } return maxNotEmpty==0;}使用的时候这样 boolean result = test(1, "hello", "", null); 亦或return ((a != null) ^ (b != null) ^ (c != null)) ^ ((a != null) & (b != null) & (c != null)); 两题面试题 一个简单的问题, java正则表达式, 提取字符 哎,要点代码吧,满,google找不到了swing的,熟悉的兄弟进来一下 求教,如何写一个像windows xp开机时的进度条? 帮帮我啊!谢谢啊!各位高手 文件的读取问题? 没有心情,散分! 3 java 连接oracle8i 数据库问题。急~~~~~~~~~~~~~~ 怎样把.java文件编译成本地代码的exe文件 对象类型可以作为变量进行强转吗? J2EE,J2SE,J2ME 初学者的提问
boolean test(string a,string b,Long c)
{
return (a!=null && b == null && c == null)
|| (a==null && b != null && c == null)
|| (a=null && b == null && c != null);
}
public boolean test(String a,String b,Long c) {
boolean [] flag = new boolean[3];//保存是否为空的结果
flag[0] = (a == null) ? true:false;
flag[1] = (b == null) ? true:false;
flag[2] = (c == null) ? true:false;
int count = 0;//计数,为空的参数个数
for(int i=0; i<flag.length; i++) {
if(flag[i])
count ++;
}
if(count == 2)
return true;
else
return false;
}
{
int i=0;
if(a!=null) i++;
if(b!=null) i++;
if(c!=null) i++;
return i==1?true:false;
}
{
if(!a==null) return true ;
if(!a==null) return true ;
if(!a==null) return true ;
}
复杂了
直接
flag[0] = (a == null);
即可
只有一个不为空,上面的反了:return ( a==null?0:1 + b==null?0:1 + c==null?0:1 ) ==1;或return ( a!=null?1:0 + b!=null?1:0 + c!=null?1:0 ) ==1;或return ( a==null?1:0 + b==null?1:0 + c==null?1:0 ) ==2;
y?
System.out.println((a == null) ^ (b == null) ^ (c == null));
for(Object str: args){
if (str != null && (String)str.trim().length() > 0){
maxNotEmpty--;
if(maxNotEmpty < 0)
return false;
}
}
return maxNotEmpty==0;
}使用的时候这样
boolean result = test(1, "hello", "", null);